Android:使用GSON的对象的JSON失败
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android:使用GSON的对象的JSON失败相关的知识,希望对你有一定的参考价值。
我是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服务提供的。
这一直困扰着我2天,所以我期待继续项目。
再次感谢
巴拉特的回答是正确的。字段名称区分大小写。
以上是关于Android:使用GSON的对象的JSON失败的主要内容,如果未能解决你的问题,请参考以下文章
android gson使用--json2对象与对象2json
如何在 Android 的 Kotlin 中使用 GSON 将 JSON 对象转换为 JSON 数组