Java 使用Android上的Gson和Kinvey反序列化自定义类
我正在使用Kinvey Android SDK从其后端检索数据。它在幕后使用Gson。我无法让它对自定义对象数组进行反序列化。以下是相关的实体定义和JSON:Java 使用Android上的Gson和Kinvey反序列化自定义类,java,android,gson,kinvey,Java,Android,Gson,Kinvey,我正在使用Kinvey Android SDK从其后端检索数据。它在幕后使用Gson。我无法让它对自定义对象数组进行反序列化。以下是相关的实体定义和JSON: public class AlbumEntity extends GenericJson { @Key("_id") private String id; @Key private String name; @Key private String location_name;
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对象数组,数据字段在未知字段列表中保持不变。如何使其适用于我的自定义对象
我尝试过的其他东西包括
列出工件
以及ArrayList和Collection。我是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;
...
我是Kinvey的工程师,在安卓图书馆工作- 在静态内部类的定义中添加一个
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;
...