![](/img/trans.png)
[英]Retrofit conversion issue - how to correctly create POJO class for concrete JSON:
[英]how do i create POJO class for json with dynamic keys for retrofit?
{
"batchcomplete": "",
"warnings": {
"main": {
"*": "Unrecognized parameter: rvprop."
},
"extracts": {
"*": "\"exlimit\" was too large for a whole article extracts request, lowered to 1."
}
},
"query": {
"normalized": [
{
"from": "pune",
"to": "Pune"
}
],
"pages": {
"164634": {
"pageid": 164634,
"ns": 0,
"title": "Pune",
"extract": ""
}
}
}
}
在上面的 json 中,“pages”对象内的数字键是动态的。 那么我如何为这个 json 制作一个 pojo。
拜托,拜托,请帮帮我。 我为此搜索了很多,但没有任何效果。 另外我是改装的初学者,所以请详细回答。 我已经看到一些提到在这种情况下使用 map 的答案(例如, 使用 Retrofit 解析动态键 Json 字符串)。 但是这些答案没有得到正确阐述。 请帮助我彻底理解它。
您可以在Pages
类中使用 A hashmap
附加属性/动态属性
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
public class Query {
private List<Address> normalized ;
private Map<String, Pages> pages;
//getter & Setter
public Map<String, Pages> getPages() {
return pages;
}
public void setPages(Map<String, Pages> pages) {
this.pages = pages;
}
public class Pages {
private String pageid;
private int ns;
private String title;
private String extract;
//define all getter and setter methods
}
public class Address{
private String from;
private String to;
}
}
//使用setter方法在Pages类中添加数据。 页面 pageObj = new Page(); pageObj.setPageid("164634"); pageObj.setns(0); ... 创建本地 Map Map pagesObj = new HashMap<>(); pagesObj.put("164634", pageObj); 使用 setPages(pagesObj); 设定值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.