简体   繁体   中英

GSON not parsing nested JSON objects properly

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);

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

}

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM