GSON not parsing nested JSON objects properly

gson deserialize nested objects
gson serialize nested objects
get nested json object with gson using retrofit
gson serializedname nested
gson nested json
gson deserialize nested class
gson flatten json
convert nested json to java object using jackson

I have some data in the form of JSON, and was using the GSON library to parse it into a Java object to be used in later portions of the code. The JSON has nested objects, which don't seem to be getting parsed properly, and I can't figure out why, as the outer object is being converted as desired. Here is an example of the JSON data I'm looking at:

{  
   "title":"Emergency Services Headquarters",
   "description":"",
   "cid":"C70856",
   "building_id":"4714",
   "building_number":"3542",
   "campus_code":"20",
   "campus_name":"Busch",
   "location":{  
      "name":"Emergency Services Headquarters",
      "street":"129 DAVIDSON ROAD",
      "additional":"",
      "city":"Piscataway",
      "state":"New Jersey",
      "state_abbr":"NJ",
      "postal_code":"08854-8064",
      "country":"United States",
      "country_abbr":"US",
      "latitude":"40.526306",
      "longitude":"-74.461470"
   },
   "offices":[  
      "Emergency Services"
   ]
}

I used codebeautify to create the Java object classes required for the JSON (everything is within Building.java):

public class Building {
    private String title;
    private String description;
    private String cid;
    private String building_id;
    private String building_number;
    private String campus_code;
    private String campus_name;
    Location LocationObject;
    ArrayList < Object > offices = new ArrayList < Object > ();

    //Setters and getters have been omitted

}

class Location {
    private String name;
    private String street;
    private String additional;
    private String city;
    private String state;
    private String state_abbr;
    private String postal_code;
    private String country;
    private String country_abbr;
    private String latitude;
    private String longitude;

    //Setters and getters have been omitted
}

Here is the code I'm using to parse the JSON, where the variable json is an input parameter for the method:

Gson obj = new Gson();
JsonArray buildingsArray = new JsonArray();
JsonParser parser = new JsonParser();
JsonElement jsonElement = parser.parse(json);
buildingsArray = jsonElement.getAsJsonArray();
for (int i = 0; i < buildingsArray.size(); i++)
    Building building = obj.fromJson(buildingsArray.get(i), Building.class);

When I call methods such as building.getTitle() or building.getCid(), I get the appropriate values, however when I do building.getLocation() (where Location is a separate object), the code returns null. I have not been able to figure it out, is it an issue with the way GSON works? Or am I doing something wrong in my code?

First of all, change:

Location LocationObject;

to:

private Location location;

And, you can deserialise JSON much easier:

Gson gson = new GsonBuilder().create();
Building building = gson.fromJson(json, Building.class);

Gson — Mapping of Nested Objects, Your browser does not currently recognize any of the video formats available. The only way in JSON (without working with IDs and binding things together Gson correctly created the nested userAddress JSON object. @BrianRoach's solution is the correct solution. It is worth noting that in the special case where you have nested custom objects that both need a custom TypeAdapter, you must register the TypeAdapter with the new instance of GSON, otherwise the second TypeAdapter will never be called.

Json property name should match your POJO class properties, it should be location not LocationObject

public class Building {
private String title;
private String description;
private String cid;
private String building_id;
private String building_number;
private String campus_code;
private String campus_name;
Location location;
ArrayList < Object > offices = new ArrayList < Object > ();

//Setters and getters have been omitted

}

Deserialize JSON to nested objects with default values � Issue , GitHub is home to over 50 million developers working together to host I'm aware that Gson works correctly for static nested classes. In my real code it's not possible to convert a non-static inner class to a static nested class. Gson correctly created the nested userAddress JSON object. Of course, we could add more nested objects for the user's payment method or work address. Even the nested objects can have nested objects! In the next part, we'll look at the other direction. How can we deserialize the complex, nested JSON into Java objects? Nested Objects Deserialization

It seems that you have a bad naming. Your location object in Building class is called LocationObject when your object inside JSON is called location.

Parsing nested Json objects in Android - DEV, GSON- A Java serialization/deserialization library to convert JSON Objects To successfully parse all those objects, we need to create their� Gson provide simple toJson() and fromJson() methods to convert Java objects to / from JSON.. toJson() – Java object to JSON Gson gson = new Gson(); // 1. Java object to JSON file gson.toJson(obj, new FileWriter("C:\\fileName.json")); // 2.

How to parse the nested JSON object, using GSON in java?, Suppose, the nested JSON object which we need to parse looks like:. “How to parse the nested JSON object, using GSON in java? The above Gson example of JSON parsing is known as Object model because whole JSON is converted to object at once. Most of the times it’s enough for us but if JSON is really huge and we don’t want to have all of it in memory at once, Gson provides Streaming API too.

Creating nested JSON objects with different formats in Java with , new JsonParser(); JsonElement jsonElement = jsonParser.parse(json); JsonElement wrapperElement = jsonElement. Creating the Java objects is not always unambiguous. Gson correctly created the nested userAddress JSON object. Same concept as earlier, we just don’t have to map this to any key from the json string, because it’s a List, not a map. Code here. JSON structure #6 : Complex nested structures. Here is page

Gson nested list, However, Gson can not automatically deserialize the pure inner classes since Related Post: Dart/Flutter – Convert/Parse JSON string, array into Object, List a proper format so both platforms understand what the data is and how to parse it. I am currently in investigation why JSON.stringify() does not properly parse my object. This is my object I am trying to parse into a JSON string: var data = [{ name: string, active: bool, data: [ value: number, date: string ] }] However when calling JSON.stringify() on my object, I get a result similar to this:

Comments
  • Thanks! Should've seen that, I checked for the other properties but somehow ignored this one smh
  • Of course it would be something so small smh. I'm upset I overlooked that, thanks!