Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generate JSON File from database data using servlet

 
ramu yadiki
Greenhorn
Posts: 13
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi friends please tell me how to create these type in JSON with Servlet.
in my database table i have one table with two columns name and value with some data. so, i want to create that databse data into one json file using servlet. the JSON file data will be like below things. please tell me how to create these json file using servlet


{
"data" : [
{
"name" : "Unit_1",
"value" : 20

},
{
"name" : "Unit_2",
"value" : 10
},
{
"name" : "Unit_3",
"value" : 30
},
{
"name" : "Unit_4",
"value" : 10
},
{
"name" : "Unit_5",
"value" : 29
}
]
}
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no API in the servlet specification for generating JSON. You should use one of the available libraries such as Gson or Jackson.
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are lucky enough to be using EE7 then you can use spec APIs http://docs.oracle.com/javaee/7/tutorial/doc/jsonp.htm but I suspect you are actually looking for something else according to http://www.coderanch.com/t/623082/Servlets/java/Create-Dynamic-JSON-Servlet
 
ramu yadiki
Greenhorn
Posts: 13
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i want that related. because i'm new for all technologies that's why i don't know the functionality and how to write that things inside servlet
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then pick a package and read through the tutorials. You're not going to get anywhere by just throwing your hands up in the air.

Let's take Gson -- here's its User's Guide. Start reading.
 
ramu yadiki
Greenhorn
Posts: 13
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i tried these code. here data is comming from database, so next i want to create these data into json format. can you tell me anyone
package com.file;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GetDataTable extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
res.setContentType("application/json");
PrintWriter pw = res.getWriter();
Connection con;
Statement stmt;
ResultSet rs;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbcracle:thin:@localhost:java","java","java");
stmt = con.createStatement();
rs = stmt.executeQuery("Select * from SERIESCHART");

pw.println("DATA "+ "VALUE" + "<br>");
while(rs.next())
{
pw.println(rs.getString(1)+" "+rs.getString(2) + "<br>");
}
}
catch (Exception e){
pw.println(e);
}
}
}
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you read through the responses you have been getting you will find advice to put the data into a POJO and then read a tutorial for a JSON API (you get to choose one) which will show you how to create the JSON (typically in a one line statement).
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read the Gson user's guide?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic