Android:使用GSON的对象的JSON失败

时间:2016-04-06 10:10:23

标签: java android json gson

我是Android开发的新手,并且有一个问题,即使用GSON 2.6.1版将JSON字符串转换为类实例。

我需要将像这样的字符串转换为对象:

{
"Messages": [{
    "ForwardMsg": true,
    "IsAdmin": true,
    "MsgBody": "Some text",
    "SysInfo": null,
    "Recipients": ["Some test"]
}, {
    "ForwardMsg": true,
    "IsAdmin": false,
    "MsgBody": "Some other text",
    "SysInfo": null,
    "Recipients": ["Some test", "Some more text"]
}]
}

以此(http://howtodoinjava.com/best-practices/google-gson-tutorial-convert-java-object-to-from-json/)为灵感,我想出了以下内容: 我有一个类DemoMessageList,如下所示:

import java.util.List;

public class DemoMessageList {

private List< DemoMessage> messages;

public DemoMessageList () {
}

public List< DemoMessage > getMessages() {
    return messages;
}

public void setMessages(List< DemoMessage > messages) {
    this.messages = messages;
}

@Override
public String toString()
{
    return "Messages ["+ messages + "]";
}
}

一个看起来像这样的类DemoMessage:

import java.util.List;

public class DemoMessage {
private Boolean forwardMsg;
private Boolean isAdmin;
private String msgBody;
private String sysInfo;
private List<String> recipients;

public Boolean getForwardMsg() {
    return forwardMsg;
}

public void setForwardMsg(Boolean forwardMsg) {
    this.forwardMsg = forwardMsg;
}

public Boolean doForwardMsg() {
    return forwardMsg;
}

public Boolean getIsAdmin() {
    return isAdmin;
}

public void setIsAdmin(Boolean isAdmin) {
    this.isAdmin = isAdmin;
}

public String getMsgBody() {
    return msgBody;
}

public void setMsgBody(String msgBody) {
    this.msgBody = msgBody;
}

public String getSysInfo() {
    return sysInfo;
}

public void setSysInfo(String sysInfo) {
    this.sysInfo = sysInfo;
}

public List<String> getRecipients() {
    return recipients;
}

public void setRecipients(List<String> recipients) {
    this.recipients = recipients;
}
}

当我这样做时,尝试变换:

public void test() {
String demoData = {"Messages": [{ "ForwardMsg": true, "IsAdmin": false,"MsgBody": "Some other text", "SysInfo": null, "Recipients": ["Some test", "Some more text"]}]}
Log.d("AsData ", "demoData: " + demoData);
Gson gson = new Gson();
DemoMessageList dmList = gson.fromJson(demoData, DemoMessageList.class);
Log.d("AsList ", "dmList: " + dmList.toString());
Log.d("ListSize ", "dmList - Size: " + String.valueOf(dmList.getMessages().size()));
}

我记录了这个:

demoData: {"Messages": [{ "ForwardMsg": true, "IsAdmin": false, "MsgBody": "Some other text", "SysInfo": null, "Recipients": ["Some test", "Some more text"]}]}
dmList: Messages [null]
dmList - Size: 0

为什么这会失败? 请帮忙!!!

4 个答案:

答案 0 :(得分:2)

您的JSON名称与您的班级字段名称不同。 GSON会查看您的字段名称以进行转换。

使用自定义命名

这样的模型类
@SerializedName("ForwardMsg")
private Boolean forwardMsg;
@SerializedName("IsAdmin")
private Boolean isAdmin;
@SerializedName("MsgBody")
private String msgBody;
@SerializedName("SysInfo")
private String sysInfo;
@SerializedName("Recipients")
private List<String> recipients;

并保留你的其他课程,

@SerializedName("Messages")
private List< DemoMessage> messages;

答案 1 :(得分:1)

在您的JSON属性名称上使用驼峰大小写:

{
"messages": [{
    "forwardMsg": true,
    "isAdmin": true,
    "msgBody": "Some text",
    "sysInfo": null,
    "recipients": ["Some test"]
}, {
    "forwardMsg": true,
    "isAdmin": false,
    "msgBody": "Some other text",
    "sysInfo": null,
    "recipients": ["Some test", "Some more text"]
}]
}

..并使字段名匹配JSON属性名称的大小写,例如:

private List<DemoMessage> messages;

简而言之:JSON属性名称必须与您的类中定义的字段匹配,包括拼写和字母大小写。

答案 2 :(得分:0)

你摇滚人!!

当我加入这个时, Bharath Mg'的答案在我的小测试中表现得非常完美:

import com.google.gson.annotations.SerializedName;

我无法控制真实世界应用程序中包含JSON的字符串,因为它是由Web服务提供的。

这一直困扰着我2天,所以我期待继续项目。

再次感谢

答案 3 :(得分:0)

巴拉特的回答是正确的。字段名称区分大小写。