【问题标题】:MySql table data to JSON ConversionMySql 表数据到 JSON 的转换
【发布时间】:2017-05-13 16:42:08
【问题描述】:

这是 MySql 表的内容。如何使用 JSP 以 JSON 格式获取此结果。

                        name      child name      child name1      child color 

                        parent        null          null           null
                         null         c1                           red          
                         null                       c11            blue
                         null                       c12            red
                         null         c2                           pink     
                         null                       c21            red
                         null                       c22            red
                         null         c23                          red   

这是我的 JSP 代码

 <%@ page import="java.sql.*" %>
<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>


<HEAD>

    <TITLE>Fetching Data From a Database</TITLE>
  </HEAD>



    <H1>Fetching Data From a Database</H1>

    <% 
         Class.forName("com.mysql.jdbc.Driver");  

    Connection con=DriverManager.getConnection
   ("jdbc:mysql://localhost:3306/data","root","admin321"); 


        Statement statement = con.createStatement();

        String id = request.getParameter("name");  

        ResultSet resultset = 
           statement.executeQuery("select * from newnew") ; 

        if(!resultset.next()) {
            out.println("Sorry, could not find . ");
        } else {  
    %>

   <% 
       } 
   %>
   [

     <% while (resultset.next()) { %> 
     { "name": "<%= resultset.getString("name") %>" , 
     "children": [{ "name": "<%= resultset.getString
   ("child name") %>",    "color":
    "<%= resultset.getString("child color") %>" ,  "children" : [
      { "name":    "<%= resultset.getString("child name1") %>", 
      "color": "<%= resultset.getString("child color") %>"


      }
      ]}




    <% } %> 

这显示格式错误,如果我将它用于它显示的另一个表。

[{ “姓名”:“父母”, “孩子们”: [{ “名称”:“c1”, “红色” }],{ “姓名”:“父母”, “红色”, “孩子们”: [{ “名称”:“c11”, “红色” }] },

}]

我想变成这样。

[  
   {  
      "name":"parent",
      "children":[  
         {  
            "name":"c1",
            "color":"red",
            "children":[  
               {  
                  "name":"c11",
                  "color":"red"
               },
               {  
                  "name":"c12",
                  "color":"red"
               }
            ]
         },
         {  
            "name":"c2",
            "color":"orange",
            "children":[  
               {  
                  "name":"c21",
                  "color":"red"
               },
               {  
                  "name":"c22",
                  "color":"red"
               },
               {  
                  "name":"c23",
                  "color":"green"
               },
            }
         ]

【问题讨论】:

    标签: mysql json jsp


    【解决方案1】:

    最简单的方法之一是使用 Google Json [https://github.com/google/gson]

    或者使用给定的代码(我在 Stackoverflow 上找到了给定的代码,它很有用)。

    public String getJSONFromResultSet(ResultSet rs,String keyName) {
    Map json = new HashMap(); 
    List list = new ArrayList();
    if(rs!=null)
    {
        try {
            ResultSetMetaData metaData = rs.getMetaData();
            while(rs.next())
            {
                Map<String,Object> columnMap = new HashMap<String, Object>();
                for(int columnIndex=1;columnIndex<=metaData.getColumnCount();columnIndex++)
                    String val= response.getString(metaData.getColumnName(columnIndex));
                    String key = metaData.getColumnLabel(columnIndex);
                    if(val== null)
                        columnMap.put(key, "");
                    else if (val.chars().allMatch(Character::isDigit))
                        columnMap.put(key,  Integer.parseInt(val));
                    else
                        columnMap.put(key,  val);
                }
                list.add(columnMap);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        json.put(keyName, list);
     }
     return JSONValue.toJSONString(json); }
    

    PS - 我使用了这两种方法所以我更喜欢你选择 Google JSON。

    【讨论】:

    • 通过mysql的任何其他方式
    • 我的意思是使用mysql和jsp
    • 您想将数据存储在 Json 中?如果是,则使用第二种方法并在声明标签中编写代码。或者简单地为此制作一个 Servlet。
    • stackoverflow.com/questions/29137915/…。在此链接中,我发现特定 id 的 json 格式直接存储在行中。我使用的是 utf8 字...是否可以每行存储 25,000 列,包含 2000 个字符
    猜你喜欢
    • 2016-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-10
    • 2021-12-17
    • 2021-08-10
    • 1970-01-01
    相关资源
    最近更新 更多