使用GSON进行嵌套的JSON对象解析

时间:2016-12-11 01:15:13

标签: json parsing object nested gson

我有一个嵌套的JSON对象,比如

{
  "hours": {
    "Friday": {
      "close": "21:00",
      "open": "11:00"
    },
    "Tuesday": {
      "close": "21:00",
      "open": "11:00"
    }
  },
  "open": true,
  "categories": [
    "Fast Food",
    "Restaurants"
  ],
  "city": "Dravosburg",
  "review_count": 7,
  "name": "Mr Hoagie",
  "neighborhoods": [

  ],
  "longitude": -79.9007057,
  "state": "PA",
  "stars": 3.5,
  "latitude": 40.3543266,
  "attributes": {
    "Take-out": true,
    "Drive-Thru": false,
    "Good For": {
      "dessert": false,
      "latenight": false
    },
    "Caters": false,
    "Noise Level": "average",
    "Takes Reservations": false,
    "Delivery": false,
    "Ambience": {
      "romantic": false,
      "intimate": false,
      "classy": false
    },
    "Parking": {
      "garage": false,
      "street": false
    },
    "Has TV": false,
    "Outdoor Seating": false,
    "Attire": "casual"
  },
  "type": "business"
}

我正在使用GSON来解析这个json对象。我创建了POJO类,如下所示:

 public class BusinessPojo {

   private String type;
   private String name;
   private ArrayList<String> neighborhoods = new ArrayList<>();
   private ArrayList<String> categories = new ArrayList<>();
   private BusinessDay hours = new BusinessDay();
   private String city;
   private boolean open;
   private String state;
   private double latitude;
   private double longitude;
   private float stars;
   private int review_count;
   private BusinessAttributes attributes;
//getters and setters
}
public class BusinessDay {
    private BusinessHourTime monday = new BusinessHourTime();
    private BusinessHourTime tueday = new BusinessHourTime();
    private BusinessHourTime wednesday = new BusinessHourTime();
    private BusinessHourTime thursday = new BusinessHourTime();
    private BusinessHourTime friday = new BusinessHourTime();
    private BusinessHourTime satday = new BusinessHourTime();
    private BusinessHourTime sunday = new BusinessHourTime();
// getters and setters
}
public class BusinessHourTime {
    private String open;
    private String close;
//getters and setters
}

Attributes类似,ParkingAmbienceGoodFor的属性内部。这是我的代码:

String line = "json object"
Gson g = new Gson();
BusinessPojo businessJsonObj = g.fromJson(line, BusinessPojo.class);

当我尝试访问"Drive-Thru"

System.out.println(businessJsonObj.getAttributes().isDriveThru());

我获得了价值false,但是当我尝试从openFriday访问Tuesday时,我得到了null

System.out.println(businessJsonObj.getHours().getTuesday().getOpen());

有人能帮我理解我在代码中犯了什么错误吗?

1 个答案:

答案 0 :(得分:0)

从风格的角度来看,给定的JSON存在一些问题。这应该考虑用于GSON映射。默认情况下,GSON将DTO类字段名称“对称地”映射到JSON字段名称(将忽略getter和setter):如果您的最顶层类字段名为foo,则给定的JSON应包含最顶层字段也称为foo,以便使用默认规则进行映射。

但是,GSON具有@SerializedName注释的名称覆盖机制。在你的情况下,它应该是:

public static final class BusinessDay {
    ...
    @SerializedName("Tuesday")
    private BusinessHourTime tueday = new BusinessHourTime();
    ...

用例(假设为了简单起见,可以直接访问字段的静态嵌套类):

System.out.println(businessJsonObj.hours.tueday.open);
  

11:00

同样地,我假设,isDriveThru()永远无法为true工作,无论JSON如何,总是返回false,因为名称Drive-Thru以资本开头字母(如果您的映射类字段也以D开头,并且其名称中包含连字符-并不重要)。由于后者在Java中不允许使用带连字符的命名问题,因此您必须明确地映射该字段:

@SerializedName("Drive-Thru")
private boolean isDriveThru;

另外,如果可能的话,我强烈建议修复JSON属性名称以摆脱非标准命名:仅仅是用于JSON属性名称的camelCase,如Friday - &gt; fridayreview_count - &gt; reviewCountTake-out - &gt; takeOutGood For - &gt; goodFor,等等。修改格式后,大多数情况下您都不需要@SerializedName注释(还要注意tueday的{​​{1}},tuesday的{​​{1}}等拼写错误,等等)。如果不可能,只需使用此注释来修复映射,如上所述。