How to parse nested JSON in Java?

Member

by craig , in category: Java , 2 years ago

How to parse nested JSON in Java?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by rachelle_funk , a year ago

@craig To parse a nested JSON object in Java, you can use the org.json library. Here's an example of how to parse a JSON object that has a nested object within it:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import org.json.JSONObject;

String jsonString = "{ "name": "John", "age": 30, "city": { "name": "New York", "state": "NY" } }";

JSONObject obj = new JSONObject(jsonString);

String name = obj.getString("name");
int age = obj.getInt("age");

JSONObject city = obj.getJSONObject("city");
String cityName = city.getString("name");
String state = city.getString("state");


This will parse the jsonString and store the values of the "name", "age", "name", and "state" fields in the name, age, cityName, and state variables, respectively.


You can also use the org.json library to parse a JSON array that has nested objects within it. Here's an example of how to do that:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import org.json.JSONArray;

String jsonString = "[{ "name": "John", "age": 30, "city": "New York" }, { "name": "Jane", "age": 25, "city": "Chicago" }]";

JSONArray array = new JSONArray(jsonString);

for (int i = 0; i < array.length(); i++) {
  JSONObject obj = array.getJSONObject(i);
  String name = obj.getString("name");
  int age = obj.getInt("age");
  String city = obj.getString("city");
  // Do something with the values
}


This will parse the jsonString and loop through each object in the array, extracting the values of the "name", "age", and "city" fields and storing them in the name, age, and city variables, respectively.

by eric.hamill , 7 months ago

@craig 

There are several libraries available in Java for parsing nested JSON. One of the popular libraries is Gson by Google.


Here's an example of how to parse a nested JSON using Gson:

  1. Add the Gson library to your project dependencies. If you are using Maven, include the following snippet in your pom.xml file:
1
2
3
4
5
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.8</version>
</dependency>


  1. Create a Java class that represents the structure of the nested JSON:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
public class Person {
    private String name;
    private int age;
    private Address address;

    // Getters and setters
}

public class Address {
    private String city;
    private String state;

    // Getters and setters
}


  1. Parse the JSON string using Gson:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import com.google.gson.Gson;

String jsonString = "{ "name": "John", "age": 30, "address": { "city": "New York", "state": "NY" } }";

Gson gson = new Gson();
Person person = gson.fromJson(jsonString, Person.class);

// Access the nested objects
String name = person.getName();
int age = person.getAge();
String city = person.getAddress().getCity();
String state = person.getAddress().getState();


In the above example, the fromJson method of Gson is used to deserialize the JSON string into a Person object.


You can access the nested objects by calling the appropriate getters on the Person object.


Note: Make sure that the field names in your Java class are exactly the same as the corresponding keys in the JSON string. If they don't match, you can use the @SerializedName annotation from the Gson library to specify the mapping between the field names and JSON keys.