Nowadays, it’s very common to receive JSON string from a Java web service instead of XML, but unfortunately, JDK (Java Development Kit) doesn’t still support conversion between String to JSON object. However, keeping JSON as String is not always a good option as you cannot operate on it readily, there is a need to convert it into JSON object before doing anything else, as an example, retrieve any field or set different values.
Must Read: Kotlin or Java? Which One is Better for Android App Development
- Gson from Google
Well, give a read to this article to know how to use these above three main libraries to do String to JSON conversions!
So, Convert String to JSON Object in Java:
String to JSON Object using Gson:
The Gson is said to be an open-source library to deal with JSON online in Java programs. Yes, Gson comes from none other than Google that is also behind Guava, (for Java programmers, it is a common purpose library). You can readily convert JSON String to JSON object in just 2 lines by using Gson as mentioned below:
Gson g = new Gson();
Player p = g.fromJson(jsonString, Player.class)
Also, you can convert Java object to JSON by using toJson() method as mentioned below:
String str = g.toJson(p);
JSON String to JSON Java object using JSON-Simple:
The JSON-simple is said to be another open-source library that supports JSON parsing and formatting. The amazing thing about this open-source library is its small size that is splendid for memory constraint environments like J2ME and Android.
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(stringToParse);
Also, another good thing about this JSON-simple that it is JDK (Java Development Kit) 1.2 compatible, which means you, can readily utilize it on a legacy project which is not yet in Java 5.
String to JSON – Jackson Example:
Jackson is the most well-known JSON parsing library in JAVA world. It is fully packed with fast, feature-rich, and supports streaming that makes its great for parsing a large JSON output from web services. Well, the mentioned one linear JSON string indicating a soccer player into a Java class representing player:
Player ronaldo = new ObjectMapper().readValue(jsonString, Player.class);
One of the major drawbacks of Jackson is that it requires JDK (Java Development Kit) 1.5 so it on you stick within earlier Java version than it may not fit there. Also, remember that Jackson doesn’t support J2ME, but it supports streaming works great to parse huge JSON response without loading it fully in memory.
Thankfully, you come to know how to convert String to JSON object in Java! However, you can account any of the JSON-simple, Gson, or Jackson simple for parsing JSON messages that you are received from web services, each of them has their own advantage. In fact, JSON-simple contains a small memory footprint, which means that it’s quite suitable for J2ME and Android client, on the other hand, Jackson is loaded with feature-rich, means so better supported for a large project. Finally, Gson is between them and one of the highly recommended JSON parser in Java programs.