0

i am using eclipse with tomcat v7. I need to display a datatable. The data are taken from mysql database. I am using java to retrive data from database. but i need to convert this data into json format. Datatable use only json data. so please help me to convert data into json format.

i need data in this format

String json = "{ \"aaData\":[[\"First Name1\",\"Last Name1\",\"Address1\",\"Address2\"],[\"First Name\",\"Last Name\",\"Address\",\"sdfsdf\"]]}";
Infinite Recursion
  • 6,511
  • 28
  • 39
  • 51
user3016102
  • 37
  • 2
  • 3
  • 2
    Create a class with the structure you mentioned, populate the values and use [Gson](http://code.google.com/p/google-gson/) to convert that object to a json. – Rahul Nov 22 '13 at 10:31

3 Answers3

0

Try with Json with Jakson library

Check this

ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();

String json = ow.writeValueAsString(object);
Murali Murugesan
  • 22,423
  • 17
  • 73
  • 120
0

you can use JPA annotations to directly fetch data into a POJO you can refer this for the same

after fetching the same you can use JAXB or any other API to convert that into JSON directly for JAXB you can refer this for json conversion

Community
  • 1
  • 1
dev2d
  • 4,245
  • 3
  • 31
  • 54
0

Try below code.

String json = "{ \"aaData\":[[\"First Name1\",\"Last Name1\",\"Address1\",\"Address2\"],[\"First Name\",\"Last Name\",\"Address\",\"sdfsdf\"]]}";

JSONParser parser=new JSONParser();

Object myJson = parser.parse(json);
Bhavesh Parekh
  • 212
  • 2
  • 11