我能够使列表映射仅@SerializedName
用于所有字段就可以使用Type
。
通过调试器运行代码(在下面的第4步中),我可以观察到该List<ContentImage> mGalleryImages
对象填充了JSON数据
这是一个例子:
1. JSON
{
"name": "Some House",
"gallery": [
{
"description": "Nice 300sqft. den.jpg",
"photo_url": "image/den.jpg"
},
{
"description": "Floor Plan",
"photo_url": "image/floor_plan.jpg"
}
]
}
2. Java类与列表
public class FocusArea {
@SerializedName("name")
private String mName;
@SerializedName("gallery")
private List<ContentImage> mGalleryImages;
}
3.列表项的Java类
public class ContentImage {
@SerializedName("description")
private String mDescription;
@SerializedName("photo_url")
private String mPhotoUrl;
// getters/setters ..
}
4.处理JSON的Java代码
for (String key : focusAreaKeys) {
JsonElement sectionElement = sectionsJsonObject.get(key);
FocusArea focusArea = gson.fromJson(sectionElement, FocusArea.class);
}