我用gson解析嵌套的json数据但返回null

时间:2016-09-19 14:33:54

标签: java android null gson

我是一个新的Android开发人员..所以我有一个问题..我用gson解析嵌套的json数据但返回null变量。请帮我。! 我的json数据是:

{"result":"success","data":{"Items":[{"id":"5b7c8950-692a-11e6-a3c9-03b4285ed321","accountName":"5b7c8950-692a-11e6-a3c9-03b4285ed321@finanskutusu.com","userId":"111903139847063022019"}],"Count":1,"ScannedCount":12}}

AccountModel.java:

public class AccountModel {

private String result;

public Data data;

public String getResult() {
    return result;
}
public void setResult(String result) {
    this.result = result;
}

public Data getData() {
    return data;
}

public void setData(Data data) {
    this.data = data;
}}

Data.java:

public class Data {

public Items[] Items;
private String Count;
private String ScannedCount;

public Items[] getItems() {
    return Items;
}

public void setItems(Items[] Items) {
    this.Items = Items;
}

public String getCount() {
    return Count;
}

public void setCount(String Count) {
    this.Count = Count;
}

public String getScannedCount() {
    return ScannedCount;
}

public void setScannedCount(String ScannedCount) {
    this.ScannedCount = ScannedCount;
}}

Items.java:

public class Items {
private String id;
private String accountName;
private String userId;

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getAccountName() {
    return accountName;
}

public void setAccountName(String accountName) {
    this.accountName = accountName;
}

public String getUserId() {
    return userId;
}

public void setUserId(String userId) {
    this.userId = userId;
}}

谢谢..

1 个答案:

答案 0 :(得分:0)

我已使用提供的模型类成功解析数据(无更改)。这是解析器程序,

Gson gson = new GsonBuilder().create(); 
    AccountModel am = gson.fromJson("{\"result\":\"success\",\"data\":    {\"Items\":[{\"id\":\"5b7c8950-692a-11e6-a3c9- 03b4285ed321\",\"accountName\":\"5b7c8950-692a-11e6-a3c9-03b4285ed321@finanskutusu.com\",\"userId\":\"111903139847063022019\"}],\"Count\":1,\"ScannedCount\":12}}", AccountModel.class);
    System.out.println(am.getResult());
    System.out.println(am.getData().getCount());
    Items i[] = am.getData().getItems();
    System.out.println(i[0].getAccountName());

输出

success
1
5b7c8950-692a-11e6-a3c9-03b4285ed321@finanskutusu.com