MySql表数据到JSON转换

时间:2016-12-29 10:57:24

标签: mysql json jsp

这是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"
               },
            }
         ]

1 个答案:

答案 0 :(得分: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。