String to JSON in Java
Nowadays, receiving data in JSON String format rather than XML is quite frequent. Java does not transform JSON String to JSON Object when dealing with JSON String. However, using the JSON String alone is not a wise choice because handling it might be challenging. We must transform JSON String to JSON Object in order to obtain values because of the evolution and demand of the technology. There are some open-source utilities that enable us to convert JSON String to JSON Object in order to fix this issue. This section includes a detailed explanation and example of how to convert a JSON String to a JSON Object. Let's have a look at JSON format before going on to the main subject.
jsonStringExpl =
{
"name" : "JavaTpoint",
"nickname" : "JTP",
"id" : 122,
"age" : 20,
"height" : 169,
"lastOverScore" : [5, 2, 4, 4, 3, 6]
}
The first two attributes in the aforementioned JSON format are Strings, the following three are numeric, and the final attribute is a JSON array.
JSON String to JSON Object Conversion
The three libraries listed below are used to translate strings into JSON objects in Java:
Use of the Gson Library
JSON-Simple Library is used.
Jackson Public Library.
Gson Library usage
Google offers a robust and open-source Java library called Gson. It is used to translate JSON Object to JSON String and JSON String to JSON String. To change a JSON String into a JSON Object, use the following line of code.
1. Gson g = new Gson();
2. Student s = g.fromJson(jsonString, Student.class)
The toJson() method also allows us to convert JSON Object to JSON String.
- String str = g.toJson(p);
Example of conversion of string to Json object using Gson Library.
JSON-Simple Library is utilized
Another free Java package that can convert JSON String to JSON Object is this one. The JSON-Simple library has the benefit of being compact. It is ideal in situations where a memory-constrained environment is crucial. Keep in mind that JDK 1.2 is also compatible. It implies that we can utilize it with a legacy project that isn't Java 5 compatible. The JSON String is transformed into a JSON Object using the following statements.
1. SONParser parser = new JSONParser();
2. JSONObject JSON = (JSONObject) parser.parse(stringToParse);
Jackson Library
Jackson Library is a well-liked and effective Java library for mapping Java objects to JSON and the other way around. It has a wide range of functions, performs quickly, and even enables streaming. Large JSON outputs from online services can be parsed by the stream without needing to load them entirely into memory. The JSON String representing a student is transformed into a Java class by the following statement.
1. Student student = new ObjectMapper().readValue(jsonString, Student.class);
Jackson library's drawback is that it needs JDK 1.5 or later. As a result, it won't fit there if we are working with an older version. The Jackson library's lack of support for J2ME is another drawback.
Exampl1.java
import org.json.*;
public class Example1
{
public static void main(String[] args)
{
String s = "{\"name\": \"JavaTpoint\", \"language\": \"Java\"}";
JSONObject json = new JSONObject(s);
System.out.println(json.toString());
String technology = json.getString("language");
System.out.println(language);
}
}
Output:
JavaTpoint
Java