I have this Json structure and I need to get the information for "myId", "name", "adress1" and "city".
{
"InformationResponse":{
"Id":"122212",
"customerSessionId":"007",
"Summary":{
"myId":1234567,
"name":"Casino",
"address1":"13 Street",
"city":"Las Vegas",
},
...
I am using Gson (Java). I created 3 Class (InformationResponse, Summary and Main)
public class Summary {
private String myId;
private String name;
private String city;
public String getMyId() {
return myId;
}
public void setMyId(String myId) {
this.myId= myId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString(){
return getMyId() + ", "+getName()+", "+getCity();
}
}
MyObject.class
public class MyObject{
private Summary summary;
public Summary getSummary() {
return address;
}
@Override
public String toString(){
sb.append("Summary="+getSummary()+"\n");
return sb.toString();
}
}
In the Main I can see my Json file and I am doing this:
// Get Gson object
Gson gson = new GsonBuilder().setPrettyPrinting().create();
// parse json string to object
MyObject myobject1 = gson.fromJson(json, MyObject.class);
But I have null everytime.
Thanks for your help!
Jean M.
试试这个@SerializedName(“ Id”)private String myId;
Gson requires a default no args constructor:
https://sites.google.com/site/gson/gson-user-guide#TOC-Object-Examples
Try adding a public no-args constructor to both your Summary and MyObject classes.
public Summary() {}
public MyObject() {}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.