用于反序列化嵌套JSON的GSON代码

时间:2012-09-28 20:58:52

标签: gson

你好我的json是这样的,我使用gson转换为Java,我创建了一个包含所有属性的顶级类和另一个具有列表的类。但是,即使数组大小正确,值也会打印为null。我做错了什么?


Gson json = new Gson();
ProductListResponse [] response = json.fromJson(str, ProductListResponse[].class);

public class ProductList {

    public String code;
    public String name;
    public String description;
    public Date lastUpdateDate;
    public String lastUpdatedBy;        

}
------------------------------------------------------------------------------------

public class ProductListResponse {

    public ProductList plist;   

}
-------------------------------------------------------------------------------------------

[
    {
        "product": {
            "code": "RA",
            "name": "Read About",
            "description": "Read About"
        }
    },
    {
        "product": {
            "code": "READ180",
            "name": "Read180",
            "description": "Read180"
        }
    },
    {
        "product": {
            "code": "RLIB",
            "name": "Read180 Library Catalog",
            "description": "Read180 Library Catalog",
            "last_udapte_date": "2010-12-07 00:00:00.0",
            "last_updated_by": "fdixon00"
        }
    },
    {
        "product": {
            "code": "EREADS",
            "name": "Read180 eReads",
            "description": "Read180 eReads"
        }
    },
    {
        "product": {
            "code": "RSKILL",
            "name": "Read180 rSkills",
            "description": "Read180 rSkills",
            "last_udapte_date": "2010-09-20 00:00:00.0",
            "last_updated_by": "fdixon00"
        }
    },
    {
        "product": {
            "code": "POOL",
            "name": "Reference Asset Pool",
            "description": "Reference Asset Pool"
        }
    },
    {
        "product": {
            "code": "SU",
            "name": "Scholastic U",
            "description": "Scholastic U"
        }
    },
    {
        "product": {
            "code": "TR8TS",
            "name": "Six Traits of Writing",
            "description": "Six Traits of Writing"
        }
    },
    {
        "product": {
            "code": "TFX",
            "name": "TrueFlix",
            "description": "TrueFlix",
            "last_udapte_date": "2012-08-20 10:47:46.0",
            "last_updated_by": "wbyler00"
        }
    }
]

1 个答案:

答案 0 :(得分:2)

这只是一个命名混乱:在ProductListResponse重命名plistproduct并且它会立即生效。