我有一个模特:
public class ItemsSet {
@SerializedName("trk")
private String mTracking;
@SerializedName("pg")
private int mPage;
@SerializedName("more")
private boolean mMoreResults;
@SerializedName("items")
private List<Item> mItems;
// I want to annotate this directly without eds object
private List<ItemSubmitter> mItemSubmitters;
...
}
和JSON:
"payload" : {
"trk" : "...",
"##type" : "PaginatedResponse`3",
"eds" : {
"##type" : "Activity.ExtendedDataSet",
"usrs" : [
{
"##type" : "SNIP",
"kind" : "connection",
"identifier" : "4033",
"name" : "James Johnson"
},
{
"##type" : "SNIP",
"kind" : "dealercontact",
"identifier" : "317564",
"name" : "Savannah Roberts"
},
{
"##type" : "SNIP",
"kind" : "lsp",
"identifier" : "89236",
"name" : "Jenny"
}
]
},
"items" : [..]
... end of json}
我正在使用GSON进行Retrofit,并希望将这些文件归入eds objetc,而不对此eds对象进行序列化。我可以直接这样做吗?我知道在iOS中我可以这样做“eds.usrs”,但在这里它不起作用。
答案 0 :(得分:1)
您需要实现自己的JsonDeserializer来解决您的问题。 请参阅此处的示例:How do I write a custom JSON deserializer for Gson?
我的例子: https://gist.github.com/alejandro-pnz/8706d672a7640875275163ef3682ef13
使用您自己的序列化程序,您可以这样做:
Gson gson = new GsonBuilder()
.registerTypeAdapter(CommentsResponse.class, new CommentsDeserializer())
.create();