File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other Java Products and Servers and the fly likes trying to work with JSON-lib to create JSON string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Products » Other Java Products and Servers
Bookmark "trying to work with JSON-lib to create JSON string" Watch "trying to work with JSON-lib to create JSON string" New topic
Author

trying to work with JSON-lib to create JSON string

Alan Shiers
Ranch Hand

Joined: Sep 24, 2003
Posts: 236
Hi there,

Hopefully there's someone here who has worked with JSON-lib before? I'm trying to create a JSON string using a 2D String array. Each String must have the format: "field_name":"field_value"

I want the output to be like:
["LASTNAME":"MacDonald", "FIRSTNAME":"Terry", "EMAIL_ADDRESS":"tmacdonald@someplace.com"],
["LASTNAME":"Steele","FIRSTNAME":"Paul","EMAIL_ADDRESS":"psteele@someplace.com"]

I've tried the following, which unfortunately doesn't output the strings properly:
String[][] array = new String[][];
array[0][0] = "\"LASTNAME\":\"MacDonald\"";
array[0][1] = "\"FIRSTNAME\":\"Terry\"";
array[0][2] = "\"EMAIL_ADDRESS\":\"tmacdonald@someplace.com\"";
...
JSONObject jsonArray = JSONArray.fromObject(array);
System.out.println( jsonObject );

This results in:
["\"LASTNAME\":\"MacDonald\"", "\"FIRSTNAME\":\"Terry\"", "\"EMAIL_ADDRESS\":\"tmacdonald@someplace.com\""],
["\"LASTNAME\":\"Steele\"","\"FIRSTNAME\":\"Paul\"","\"EMAIL_ADDRESS\":\"psteele@someplace.com\""]

Escaping the quote symbol obviously isn't the answer here. What do I need to do to get the format I'm looking for? Please advise.

Alan
dharmendra gahlot
Ranch Hand

Joined: Apr 22, 2009
Posts: 54
Hi,

You can use GSON library. This is pretty good for formatting.


Dharmendra Gahlot
SCJP-5.0 87%,SCWCD-5.0 92%
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: trying to work with JSON-lib to create JSON string
 
Similar Threads
Java/JSON/jQuery issue
how to get an applet value in json as output
JSON response to populate listbox
Searching google images from java code
Sending and Retrieving JSON data from Jersey API