【问题标题】:Nested JSON object parsing using GSON使用 GSON 解析嵌套 JSON 对象
【发布时间】:2016-12-11 01:15:12
【问题描述】:

我有一个嵌套的 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,但是当我尝试从FridayTuesday 访问open 时,我得到null 之类的

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

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

【问题讨论】:

    标签: json parsing object nested gson


    【解决方案1】:

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

    但是,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 属性名称仅使用驼峰命名法,例如 Friday -> fridayreview_count - > reviewCount, Take-out -> takeOut, Good For -> goodFor, 等等。固定格式后,大多数情况下您不需要 @SerializedName 注释(还要注意诸如 tueday 用于 tuesday 的拼写错误,satday 用于 saturday 等)。如果不可能,只需使用此注解来修复上述映射。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多