By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,483 Members | 1,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,483 IT Pros & Developers. It's quick & easy.

Mysql data into JSON using JSP

P: 1
i am trying to get data from the mysql table in JSON format..

my jsp code
Expand|Select|Wrap|Line Numbers
  1.  <%@ 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> <% 
  2.              Class.forName("com.mysql.jdbc.Driver");  
  3.  
  4.       Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/tree","root","admin321"); 
  5.  
  6.  
  7.             Statement statement = con.createStatement();
  8.  
  9.             String id = request.getParameter("name");  
  10.  
  11.             ResultSet resultset = 
  12.                statement.executeQuery("select * from bitab") ; 
  13.  
  14.             if(!resultset.next()) {
  15.                 out.println("Sorry, could not find . ");
  16.             } else {  
  17.         %> <% 
  18.            } 
  19.        %>
  20.        [
  21.  
  22.          <% while (resultset.next()) { %> 
  23.  
  24.   { "name": "<%= resultset.getString("parent") %>" ,   "children": [{ "name": "<%= resultset.getString("child1") %>"},
  25.  
  26.     "children": [{ "name": "<%= resultset.getString("child2") %>"},
  27.    { "name": "<%= resultset.getString("child3") %>"}   ,
  28.      "children": [{ "name": "<%= resultset.getString("child4") %>"}
  29.     ] },
  30.              ] },
  31.                              ]},
  32.                              ]
  33.  
  34.  
  35.  <% } %> ;
  36.  
i cant able to get correct json format and also the null values includes as child in json..any suggestions would be helpfull..
can anyone suggest me a way to get the json as below
Expand|Select|Wrap|Line Numbers
  1. {  
  2.    "name":"par1",
  3.    "children":[  
  4.       {  
  5.          "name":"chil 1",
  6.          "children":[  
  7.             {  
  8.                "name":"chil 11"
  9.             },
  10.             {  
  11.                "name":"chil 12"
  12.             },
  13.             {  
  14.                "name":"chil 2",
  15.                "children":[  
  16.                   {  
  17.                      "name":"chil 21"
  18.                   },
  19.                   {  
  20.                      "name":"chil 22"
  21.                   }
  22.                ]
  23.             }
  24.          ]
  25.       }
  26.    ]
  27. }
Dec 30 '16 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.