2016-12-29 269 views
0

這是MySql表格的內容。如何使用JSP以JSON格式獲得此結果。將MySql表格數據轉換爲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

最簡單的方法之一是使用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 - 我用這兩種方法所以我更喜歡你選擇谷歌JSON。

+0

感謝您的回覆... – priya

+0

任何其他方式通過mysql – priya

+0

我的意思是使用mysql和jsp – priya