当我使用JSON从GSON创建对象时,如何解决这个问题?

时间:2014-01-27 19:30:42

标签: android json gson

我得到以下JSON

 {
_id: "5252fdf424f1e7fbf7000004",
address: "Calle 1000",
city: "Concepción",
created_at: "2013-10-07T18:31:19.375Z",
description: "",
name: "Joctos",
phone: "94967994",
updated_at: "2013-12-09T13:03:07.328Z",
happy_hour: {
    active: false,
    type: 1,
    all_day: false,
    start: "2013-12-17T03:30:00.000Z",
    end: "2013-12-17T05:00:00.000Z"
}

}

我认为其属性如下的Store对象

public class StoreModel {

@SerializedName("_id")
private String _id;

@SerializedName("address")
private String address;

@SerializedName("city")
private String city;

@SerializedName("created_at")
private String created_at;

@SerializedName("description")
private String description;

@SerializedName("name")
private String name;

@SerializedName("phone")
private String phone;

@SerializedName("updated_at")
private String updated_at;

@SerializedName("happy_hours")
private HappyHour happyHours;

//(获得一套)

我将HappyHour对象定义如下

public class HappyHour {
@SerializedName("active")
private String active;
@SerializedName("type")
private double type;
@SerializedName("all_day")
private String all_day;

@SerializedName("start")
private String start;
@SerializedName("end")
private String end;

//(获取和设定) }

但是当我运行程序时出现以下错误

at com.example.adicionalesprueba.StoreActivity $ 1.onItemClick(StoreActivity.java:127)

127:Log.i(“开始”,_ response.get(position).getHappyHours()。getStart());

2 个答案:

答案 0 :(得分:3)

在你的json中你有“happy_hour”,你将它作为“happy_hours”映射到你的对象中。 将@SerializedName("happy_hours")更改为@SerializedName("happy_hour")

答案 1 :(得分:0)

你的对象中有一个拼写错误。

@SerializedName("happy_hour")
private HappyHour happyHours;

它应该是小时而不是几小时