我需要使用android中的 retrofit2 从 JSON 生成 POJO ,但是问题是,标记会不时地发生变化,因为它提供了不同的查询。
JSON 1
{ "query":{
"pages":{
"4803":{
"pageid":4803,"ns":0,"title":"hello","extract":"Greating to some one"
}}}}
JSON 2
{ "query":{
"pages":{
"2354":{
"pageid":2354,"title":"nice","extract":"Say Appreciation to someone"
}}}}
在上述2个JSON中,值正在更改,例如 4803 和 2354 相似,我不了解所有值是否已精简,因此如何为此尝试生成POJO在JSONtoPOJO上,但是它以 _4803 和 _2354 的名称创建了类,但是通过向API提供不同的查询实际上改变了值。
这是我的所有Java代码 1。
public class Example {
@SerializedName("query")
@Expose
private Query query;
public Query getQuery() {
return query;
}
public void setQuery(Query query) {
this.query = query;
}
}
2。
public class modelClass {
@SerializedName("pageid")
@Expose
private Integer pageid;
@SerializedName("ns")
@Expose
private Integer ns;
@SerializedName("title")
@Expose
private String title;
@SerializedName("extract")
@Expose
private String extract;
public Integer getPageid() {
return pageid;
}
public void setPageid(Integer pageid) {
this.pageid = pageid;
}
public Integer getNs() {
return ns;
}
public void setNs(Integer ns) {
this.ns = ns;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getExtract() {
return extract;
}
public void setExtract(String extract) {
this.extract = extract;
}
}
3。
public class Pages {
@SerializedName("pages")
@Expose
private Map<String,modelClass> page;
public Map<String,modelClass> getPage() {
return page;
}
public void setPage(Map<String,modelClass> page) {
this.page = page;
}
}
5。
public class Query {
@SerializedName("pages")
@Expose
private Pages pages;
public Pages getPages() {
return pages;
}
public void setPages(Pages pages) {
this.pages = pages;
}
}
6。从mainactivity调用,但返回nullpointerException,以便如何确定值的响应。
ApiService apiService = RetroClient.getapiService();
Call<Example> call = apiService.getWordList(//here i passed value,that return json//);
call.enqueue(new Callback<Example>() {
@Override
public void onResponse(Call<Example> call,Response<Example> response) {
if (response.body() != null) {
// how get json value here
}
}
@Override
public void onFailure(Call<Example> call,Throwable t) {
}
});
请告诉我如何解决此问题。谢谢。