How to create a JSON document using Java? - java

I want to read data from the database, convert it to docs (JSON) using Java.
Thanks

GSON is a Java library from Google to convert Java objects to JSON. You can simply pass a Java object to the library function and it will return a JSON string.
Download: http://code.google.com/p/google-gson/
Example: http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/
I have used the library from http://www.json.org, but the whole thing seems to be tedious to me. GSON is simple to use IMHO.

I would use JSONObject class:
http://www.json.org/javadoc/org/json/JSONObject.html
or you can build the string.

Related

How to convert JsonForm to Json?

I would like to extract only the data present in a Json to JsonForm using Java.
Which framework can I use to this operation?
I've found the answer. Don't let the ugly aspect of JsonForm deceive you. I don't know if this apply for all cases, but I just had to extract the Json object that contains only the data. This can be done using any common Json parser, as like as Gson.

How to parse a JSON object, update it in place and then save it back to disk in Java?

What is the easiest to use JSON Java library to parse a JSON (It's structure may vary so I can't mapp it to a Java class as I seen multiple libraries do it) update some elements in an array in this JSON and then save it back to disk?
There are so many libraries for JSON in Java (Gson, Jackson, etc.) and so complex that seem like a total overkill for what i need as opposed to other programming languages.
What's the most straightforward one to use? (that maybe also has a few examples on how to do this)
I have had great success with Json-Simple.
You can check it out here.
I used it to parse 1.5 Million Twitter Streaming data (which is in JSON).
You may find some sample code here on my blog.
You can use java-json jar. The docs for this jar can be found here
I use net.sf.json to do this.
here is an example :
String fromFile="{\"he\",\"hello\"}" //read from file instead
JSONObject object=(JSONObject)JSONSerializer.toJSON( fromFile );
object.put("he", "hello2");
System.out.println(object);
output:
{"he":"hello2"}

How can i read my Json String from Delphi in Java

my Json Strin is
{"result":[[{"Name":"Mos","Family":"Hos"},{"Name":"Mos","Family":"Hos"}]]}
i connect to rest server and get this string in java but i cant pars this string to a class in java with gson .
what class i need in Java for read this string.
JSon is a format that doesn't depend on the language. You can create it in Delphi/C/even code by yourself and read in any other language.
In Java its easy because there are a lot of thirdparties that handle this.
To name a few:
Jackson
JSON
JavaJSon
FlexJson
I'm sure there are others. I think the most widespread are gson and jackson
Hope this helps
you can use jackson api for this. may this link will help to achieve this.

How one can represent the JSON Object in clean POJO?

I'm very new to Java. I'm just parsing a string and getting the Json Response like this:
{
"customer_id": "user",
"merchantId": "xxxx",
"cards":
[
{
"card_token": "715fc10a-e7b3-48a1-b6e7-09e71ac050f8",
"card_number": "11111111",
"card_isin": "23232",
"card_exp_year": "2013",
"card_exp_month": "12"
}
]
}
For some reason I wish I could be able to represent this in a POJO. Note that the cards field can consist of more than 1 field.
I'm new to Java. I don't want the code for doing it, but I want to know what is the best way to represent these structure in POJO.
You can use GSON that can easily convert json to java object (generic) and vice versa which further you can use for your POJO.
- Manual parsing of JSON to object will be a pain.
- Its better to go with Jackson, which i use.
- Or you can also choose GSON (created by google for its internal use initially).
See this link for implementation example:
http://www.mkyong.com/java/json-simple-example-read-and-write-json/
The two dominate JSON processors for Java are GSON (as #Abu points out in his answer) and Jackson. These libraries will help you map a Java POJO to a JSON object, and vice-versa.
Here is a comparison on SO.
You can also use Jackson JSON. It's a bit faster and it works great with the Java API for RESTful Services (JAX-RS), the Java standarization for REST api's.
Here's a better comparison between the two: Jackson Vs. Gson
To read an object from String, checkout the ObjectMapper class.

How to pass an array of objects from javascript to Java

Hello Im trying to pass an array of objects from javascript to java , but how can this be done..??
I've found in some posts that they do this using a hidden input. Is this the only way?
I'm a bit confused. Please tell me what do I need to do to pass my array to the server? and which javascript files and jars do I need to add?
Thanks in advance.
You seem to want a completely baked-in solution. Not sure I can provide that, but here's what I'd do.
Indeed use a hidden input field in a form, where the value of the field is a valid JSON string. Send the form to your server, and in your servlet use a JSON Java library to parse the JSON string.
Here json-lib, gson or Jackson would do. In your case, I'd say json-lib would seem the easiest to use.
To generate the JSON string on the client-side, either use a framework or custom solution. For instance, jQuery has a serialize() function to serialize a form's fields to a JSON object directly, which you can then convert to string. Other frameworks provide similar functions.
To learn more about JSON, be sure to read the JSON Wikipedia entry and to visit the official JSON page (which also gives you a Java implementation of the JSON data-interchange format, though maybe not the most efficient one for processing a lot of data). To make sure your generated JSON is valid, you can use JSONLint.
If the objects are simple enough, you can encode your array as a JSON string. Java has libraries to encode and decode JSON.

Categories