简体   繁体   English

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

[英]Android: JSON to objects using GSON fails

I'm a newbee to Android development and have a question about getting a JSON string transformed to class instances using GSON, version 2.6.1. 我是Android开发的新手,并且有一个有关使用GSON 2.6.1版将JSON字符串转换为类实例的问题。

I have a need for transforming a string like this to objects: 我需要将这样的字符串转换为对象:

{
"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"]
}]
}

With this ( http://howtodoinjava.com/best-practices/google-gson-tutorial-convert-java-object-to-from-json/ ) as inspiration, I've come up with the following: I have a class DemoMessageList that looks like this: 以此为灵感( 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 + "]";
}
}

And a class DemoMessage that looks like this: 和一个类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;
}
}

When I do this, to try transform: 当我这样做时,尝试转换:

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()));
}

I get this logged: 我得到这个记录:

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

Why does this fail?? 为什么这失败了? Please help!!! 请帮忙!!!

Your JSON names are different from your class field names. JSON名称与类字段名称不同。 GSON looks at your field names for transformation. GSON会查看您要转换的字段名称。

Use your model class like this with custom naming, 像这样使用您的模型类进行自定义命名,

@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;

and keep your other class , 并保持你的其他课程,

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

Use camel case on your JSON property names: 在您的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"]
}]
}

.. and make the fieldnames match the case of the JSON property names, eg: ..并使字段名称与JSON属性名称的大小写匹配,例如:

private List<DemoMessage> messages;

In short: The JSON property names must match the fields defined in your class(es), both by spelling and letter case. 简而言之:JSON属性名称必须通过拼写和字母大小写与您在类中定义的字段匹配。

You rock guys!! 你们这些家伙!

Bharath Mg' answer works perfect in my little test, when adding this: 在添加以下内容后,Bharath Mg的答案在我的小测验中非常完美:

import com.google.gson.annotations.SerializedName;

I have no control over the string containing the JSON in the real world application as it is provided by a webservice. 我无法控制真实应用程序中包含JSON的字符串,因为它是由Web服务提供的。

This has been bugging me for the 2 days, so I look forward to get on with project. 这已经困扰了我两天,所以我期待着继续进行下去。

Thanks again 再次感谢

Bharat's answer is correct. 巴拉特的答案是正确的。 Fields names are case-sensitive. 字段名称区分大小写。

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

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