1

我正在使用 Kinvey Android SDK 从他们的后端检索数据。它在幕后使用 Gson。我一直无法让它反序列化一组自定义对象。这是实体定义和有问题的 JSON:

public class AlbumEntity extends GenericJson {
    @Key("_id")
    private String id;

    @Key
    private String name;

    @Key
    private String location_name;

    @Key
    private String start_date;

    @Key("artifacts")
    private Artifact[] artifacts;

    static class Artifact extends GenericJson { 
        @Key
        private String type;
        @Key
        private String photo_url;
        public Artifact() {}
    }

    @Key("_kmd")
    private KinveyMetaData meta; 

    public TripketEntity() {}
}

JSON 正文:

{
"_id": "5216fec12f7b521a26064f9d",
"_kmd": {
    "ect": "2013-08-26T06:51:00.283Z",
    "lmt": "2013-09-05T15:28:28.079Z"
},
"artifacts": [
    {
        "type": "photo",
        "photo_url": "http://www.califliving.com/title24-energy/images/sanfrancisco.jpg"
    },
    {
        "type": "photo",
        "photo_url": "http://www.sanfrancisco.net/pictures/san-francisco.jpg"
    },
    {
        "type": "photo",
        "photo_url": "http://a2.cdn-hotels.com/images/themedcontent/en_GB/San%20Francisco_Top%2010.jpg"
    },
    {
        "type": "photo",
        "photo_url": "http://sanfranciscoforyou.com/wp-content/uploads/2010/03/san-francisco-city.jpg"
    }
],
"location_name": "San Francisco",
"name": "My Trip to the Bay",
"start_date": "06/15/2013",
"owner": {
    "_type": "KinveyRef",
    "_collection": "user",
    "_id": "5216fcd60b36c57d69000529"
},
"_acl": {
    "creator": "kid_ePPs9jXc_5"
}
}

如果我将private Artifact[] artifacts对象更改为private GenericJson[] artifacts,则该数组将被反序列化为 GenericJson 对象数组,而未知字段列表中的数据字段完好无损。如何让它适用于我的自定义对象?

我尝试过的其他东西包括List<Artifact> artifactsArrayList 和 Collection。

4

1 回答 1

1

我是 Kinvey 的一名工程师,致力于 Android 库-

在静态内部类的定义中添加一个public修饰符,GSON 应该能够获取它:

...

@Key("artifacts") 
private Artifact[] artifacts;

public static class Artifact extends GenericJson {  
    @Key 
    private String type;
    @Key 
    private String photo_url;
    public Artifact() {} 
} 

@Key("_kmd") 
private KinveyMetaData meta;

... 
于 2013-09-05T19:22:32.690 回答