简体   繁体   English

GSON无法正确解析嵌套的JSON对象

[英]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. 我有一些JSON形式的数据,并且正在使用GSON库将其解析为一个Java对象,以在代码的后续部分中使用。 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. JSON具有嵌套的对象,这些对象似乎无法正确解析,而且我无法弄清楚原因,因为外部对象正在按需转换。 Here is an example of the JSON data I'm looking at: 这是我正在查看的JSON数据的示例:

{  
   "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): 我使用codebeautify创建了JSON所需的Java对象类(所有内容都在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: 这是我用来解析JSON的代码,其中变量json是该方法的输入参数:

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. 当我调用诸如building.getTitle()或building.getCid()之类的方法时,我得到了适当的值,但是当我进行building.getLocation()(其中Location是一个单独的对象)时,代码返回null。 I have not been able to figure it out, is it an issue with the way GSON works? 我一直无法弄清楚,这与GSON的工作方式有关吗? 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: 而且,您可以更轻松地反序列化JSON

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 Json属性名称应该与您的POJO类属性匹配,它应该是location而不是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 . 建筑类您的位置对象被称为LocationObject里面的时候你的JSON对象被称为location

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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