Gson: convert String to JsonObject without POJO example shows how to convert String to JsonObject without using the POJO class using the Gson library in Java.
How to convert String to JsonObject without POJO?
The Google Gson library provides the fromJson
method that converts given String JSON data to various types of objects or types. We can convert the JSON data to a Java POJO (Plain Old Java Object) or we can directly convert String to JsonObject with no POJO using the same fromJson
method.
Here is the JSON we want to convert to a JsonObject.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
{ "Id": "EMP002", "Name": "John", "Age": 41, "IsManager": true, "ReservedParking": "PARK001", "Languages": ["English", "German", "Italian"], "Experience": [ { "CompanyName": "AA Ltd.", "Years": 12 }, { "CompanyName": "BB Ltd.", "Years": 9 } ] } |
Here is an example program to do that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
package com.javacodeexamples.gsonexamples; import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; public class GsonStringToJsonObjectWithoutPojo { public static void main(String[] args) { String jsonData = "{\"Id\": \"EMP002\"," + "\"Name\": \"John\"," + "\"Age\": 41," + "\"IsManager\": true," + "\"ReservedParking\": \"PARK001\"," + "\"Languages\": [\"English\", \"German\", \"Italian\"]," + "\"Experience\": [{\"CompanyName\": \"AA Ltd.\",\"Years\": 12},{\"CompanyName\": \"BB Ltd.\",\"Years\": 9}]}"; /* * To convert directly String to JsonObject, * use the fromJson method of the Gson class * and pass the JsonObject.class as the type. */ JsonObject jsonObject = new Gson().fromJson(jsonData, JsonObject.class); /* * access JsonObject properties */ //access name as string System.out.println("Name: " + jsonObject.get("Name").getAsString()); //access age as an int System.out.println("Age: " + jsonObject.get("Age").getAsInt()); //access IsManager as boolean System.out.println("IsManager: " + jsonObject.get("IsManager").getAsBoolean()); //access Languages as an array JsonArray arrayLanguages = jsonObject.get("Languages").getAsJsonArray(); //iterate array for(JsonElement element : arrayLanguages) { System.out.println(element.getAsString()); } //get Experience as an Array JsonArray arrayExperience = jsonObject.get("Experience").getAsJsonArray(); System.out.println("Companies =>"); //iterate array for(JsonElement element : arrayExperience) { //get first experience as an object JsonObject jsonObjectExperience = element.getAsJsonObject(); System.out.println("CompanyName: " + jsonObjectExperience.get("CompanyName").getAsString()); System.out.println("Years: " + jsonObjectExperience.get("Years").getAsString()); } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 |
Name: John Age: 41 IsManager: true English German Italian Companies => CompanyName: AA Ltd. Years: 12 CompanyName: BB Ltd. Years: 9 |
Please let me know your views in the comments section below.