簡體   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