It's real mutual nowadays to have JSON String from a Java spider web service instead of XML, but unfortunately, JDK doesn't nevertheless back upward conversion betwixt JSON String to JSON object. Keeping JSON equally String ever is non a practiced pick because you lot cannot function on it easily, you lot request to convert it into JSON object before you lot do anything else e.g. hollo upward whatever champaign or gear upward unlike values. Fortunately, at that topographic point are many opened upward origin library which allows you lot to do JSON object from JSON formatted String e.g. Gson from Google, Jackson, too json-simple. In this tutorial, you lot volition acquire how to exercise these three original libraries to do this conversion amongst mensuration yesteryear mensuration examples.
Even though you lot tin exercise a uncomplicated or complex JSON String e.g. amongst lots of attributes too JSON arrays, I'll exercise next JSON String for instance purpose:
jsonString = {
"name" : "Ronaldo",
"sport" : "soccer",
"age" : 25,
"id" : 121,
"lastScores" : [ 2, 1, 3, 5, 0, 0, 1, 1 ]
}
It's simple, has five attributes, ii of which are String too other ii are numeric. One attribute, lastScore is a JSON array.
You tin likewise convert a Java object to JSON yesteryear using toJson() method equally shown below
The practiced affair close Gson is that its characteristic rich too comes from Google, which is known for performance. Btw, if you lot are non real familiar amongst JSON format itself, I would advise reading Beginning JSON yesteryear Ben Smit to acquire more. It's a must that you lot know around basics of JSON before encoding too decoding Java objects to JSON.
The practiced affair close json-simple is that it is likewise JDK 1.2 compatible, which agency you lot tin exercise it on a legacy projection which is non nevertheless inward Java 5.
One of the drawbacks of Jackson is that it requires JDK 1.5 too then if you lot are stuck inward before Java version than it may non agree there. Also, Jackson doesn't back upward J2ME, but 1 of the original advantages of using Jackson is that it supports streaming which tin live used to parse huge JSON answer without loading it fully inward memory.
You tin read to a greater extent than close how to parse JSON String to Java object using these libraries on Developing RESTful Services amongst JSON, 1 of the practiced books which explicate how to exercise eat JSON information from spider web services.
That's all close how to convert String to JSON object inward Java. You tin exercise whatever of the json-simple, Gson or Jackson for parsing JSON messages received from spider web services, each of them has their ain advantage. Json-simple has small-scale retentiveness footprint agency it's quite suitable for J2ME too Android client, piece Jackson is characteristic rich too then improve supported for a large project. Gson is inward betwixt them too my favorite full general exercise JSON parser inward Java programs.
Other JSON tutorials for Java programmers
Even though you lot tin exercise a uncomplicated or complex JSON String e.g. amongst lots of attributes too JSON arrays, I'll exercise next JSON String for instance purpose:
jsonString = {
"name" : "Ronaldo",
"sport" : "soccer",
"age" : 25,
"id" : 121,
"lastScores" : [ 2, 1, 3, 5, 0, 0, 1, 1 ]
}
It's simple, has five attributes, ii of which are String too other ii are numeric. One attribute, lastScore is a JSON array.
String to JSON Object using Gson
The Gson is an opened upward origin library to bargain amongst JSON inward Java programs. It comes from none other than Google, which is likewise behind Guava, a mutual exercise library for Java programmers. You tin convert JSON String to Java object inward but 2 lines yesteryear using Gson equally shown below :Gson g = new Gson(); Player p = g.fromJson(jsonString, Player.class)
You tin likewise convert a Java object to JSON yesteryear using toJson() method equally shown below
String str = g.toJson(p);
The practiced affair close Gson is that its characteristic rich too comes from Google, which is known for performance. Btw, if you lot are non real familiar amongst JSON format itself, I would advise reading Beginning JSON yesteryear Ben Smit to acquire more. It's a must that you lot know around basics of JSON before encoding too decoding Java objects to JSON.
JSON String to Java object using JSON-Simple
The JSON-Simple is around other opened upward origin library which supports JSON parsing too formatting. The practiced affair close this library is its small-scale size, which is perfect for retentiveness constraint environments similar J2ME too Android.JSONParser parser = new JSONParser(); JSONObject json = (JSONObject) parser.parse(stringToParse);
The practiced affair close json-simple is that it is likewise JDK 1.2 compatible, which agency you lot tin exercise it on a legacy projection which is non nevertheless inward Java 5.
String to JSON - Jackson Example
Jackson is I estimate the most pop JSON parsing library inward Java world. It's fast, characteristic rich too supports streaming which is keen for parsing large JSON output from spider web services. Following 1 liner convert JSON string representing a soccer thespian into a Java shape representing player:Player ronaldo = new ObjectMapper().readValue(jsonString, Player.class);
One of the drawbacks of Jackson is that it requires JDK 1.5 too then if you lot are stuck inward before Java version than it may non agree there. Also, Jackson doesn't back upward J2ME, but 1 of the original advantages of using Jackson is that it supports streaming which tin live used to parse huge JSON answer without loading it fully inward memory.
You tin read to a greater extent than close how to parse JSON String to Java object using these libraries on Developing RESTful Services amongst JSON, 1 of the practiced books which explicate how to exercise eat JSON information from spider web services.
That's all close how to convert String to JSON object inward Java. You tin exercise whatever of the json-simple, Gson or Jackson for parsing JSON messages received from spider web services, each of them has their ain advantage. Json-simple has small-scale retentiveness footprint agency it's quite suitable for J2ME too Android client, piece Jackson is characteristic rich too then improve supported for a large project. Gson is inward betwixt them too my favorite full general exercise JSON parser inward Java programs.
Other JSON tutorials for Java programmers
- 5 JSON libraries Java JEE Programmer should know (list)
- How to parse JSON to/from Java Object using Jackson? (tutorial)
- How to convert JSON array to String array inward Java using Gson? (tutorial)
- How to parse large JSON file using Jackson Streaming API? (example)
- How to exercise Google Protocol Buffer (protobuf) inward Java? (tutorial)
- Top 10 RESTful Web Service Interview Questions (see here)
- What is the exercise of unlike HTTP methods inward REST? (see here)
References

0 Response to "3 ways to convert String to JSON object inwards Java?"
Posting Komentar