繁体   English   中英

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

[英]Android: JSON to objects using GSON fails

我是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

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

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;

在您的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属性名称必须通过拼写和字母大小写与您在类中定义的字段匹配。

你们这些家伙!

在添加以下内容后,Bharath Mg的答案在我的小测验中非常完美:

import com.google.gson.annotations.SerializedName;

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

这已经困扰了我两天,所以我期待着继续进行下去。

再次感谢

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM