. Advertisement .
..3..
. Advertisement .
..4..
This tutorial will discuss how to convert string to JSON objects in Java in three methods. There are also various libraries to perform the task.
How To Convert String To JSON Objects In Java
Method 1: Use GSon
GSon is an open-source library which can deal with JSON effectively in Java programs. This program allows users to convert raw JASON into smaller data structures and classes.
First, you need to include the dependency in pom.xml:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
From there, you can use
either JSonParser or fromJSon to convert a specific JSON String into an object:
- With JSonParser:
In the first place, it is vital to parse the original java string with the following input:
public JsonElement parse(String json) throws JsonSyntaxException
Once having String in a JSonElement tree, you should use the getAsJsonObject() method:
String json = "{ "name": "Baeldung", "java": true }";
JsonObject jsonObject = new JsonParser().parse(json).getAsJsonObject();
Assert.assertTrue(jsonObject.isJsonObject());
Assert.assertTrue(jsonObject.get("name").getAsString().equals("Baeldung"));
Assert.assertTrue(jsonObject.get("java").getAsBoolean() == true);
- With fromJSon:
public <T> T fromJson(String json, Class<T> classOfT) throws JsonSyntaxException
This method also allows for parsing a JSOn String:
String json = "{ "name": "Baeldung", "java": true }";
JsonObject convertedObject = new Gson().fromJson(json, JsonObject.class);
Assert.assertTrue(convertedObject.isJsonObject());
Assert.assertTrue(convertedObject.get("name").getAsString().equals("Baeldung"));
Assert.assertTrue(convertedObject.get("java").getAsBoolean() == true);
Method 2: Use JSON-Simple Library
JSON-Simple library is a great option thanks to its small size, especially with the memory constrained environment. Use the statements to convert to a JSON object:
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(stringToParse);
Method 3: Use Jackson Library
The Jackson library is widely used to map JSON objects to Java and vice-versa. This feature is fast in performance, rich in features, and supports streaming. Here is the statement to convert a JSON string to a Java class:
Student student = new ObjectMapper().readValue(jsonString, Student.class);
The only disadvantage of this method is that it requires more than JDK 1.5. Therefore, you will need to use an updated version.
Converting JSON String To Object Example
Input:
// Java Program to demonstrate the
// conversion of String to JSON object
import com.google.gson.*;
class GFG {
int gfgId;
String username;
char gender;
public GFG()
{
this.gfgId = 0;
this.username = "";
this.gender = ' ';
}
}
public class GFGMain {
public static void main(String arg[])
{
GFG gfg = null;
// creating JSON String of GFG class object
String jsonString;
jsonString = "{";
jsonString += "gfgId : 10001,";
jsonString += "username : 'Jack jon',";
jsonString += "gender : 'M'";
jsonString += "}";
// creating object of gson
Gson gson = new Gson();
// converting jsonStrig into object
gfg = gson.fromJson(jsonString, GFG.class);
System.out.println("GFG id of User : " + gfg.gfgId);
System.out.println("Username : " + gfg.username);
System.out.println("Gender : " + gfg.gender);
}
}
Output:
C: \gfArt\stringToJson>java -chasspath gson-2.8.7.jar; GFMain GFG id of User: 1001
Username: Jack jon
Gender: M
Conclusion
Converting JSON elements in Java is not easy for beginners. However, our tutorial offers three easiest-to-follow methods on how to convert string to JSON objects in Java. Bear in mind that the string should be in JSON format with the name pair value.
Leave a comment