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.
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:
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:
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!!!