【发布时间】:2014-06-24 22:17:59
【问题描述】:
我是 POJO 的新手,我正在努力创建对象来表示来自 Google API 的 JSON。 如何为这个 JSON 创建一个 pojo?
"notificationSettings": {
"notifications": [
{
"type": "eventCreation",
"method": "email"
},
{
"type": "eventChange",
"method": "email"
},
{
"type": "eventCancellation",
"method": "email"
},
{
"type": "eventResponse",
"method": "email"
}
]
}
我已经弄清楚了 JSON 的其余部分,但是 Gson.fromJSON() 一直返回 null。
编辑 1
这就是我所拥有的
private static class NotificationSettings{
public NotificationSettings(){
}
public NotificationSettings(Map<String, List<Notification>> notifications) {
super();
this.setNotifications(notifications);
}
public Map<String, List<Notification>> getNotifications() {
return notifications;
}
public void setNotifications(Map<String, List<Notification>> notifications) {
this.notifications = notifications;
}
private Map<String, List<Notification>> notifications;
}
private static class Notification{
public Notification(){
}
public Notification(String type, String method) {
super();
this.type = type;
this.method = method;
}
private String type;
private String method;
/**
* @return the type
*/
public String getType() {
return type;
}
/**
* @param type the type to set
*/
public void setType(String type) {
this.type = type;
}
/**
* @return the method
*/
public String getMethod() {
return method;
}
/**
* @param method the method to set
*/
public void setMethod(String method) {
this.method = method;
}
}
编辑 2 另一个问题,是否需要带参数的构造函数?另外,是不是也需要toString()方法?
谢谢!
【问题讨论】:
标签: java json gson google-calendar-api pojo