如何根据动态 JSON 生成普通旧 Java 对象

How to Generate Plain Old Java Objects from following dynamic JSON

我需要在 retrofit2 中使用 retrofit2JSON 生成 POJO =68=],但问题是通过提供不同的查询,标签会不时发生变化。

JSON 1

{ "query":{
      "pages":{
         "4803":{
            "pageid":4803,
            "ns":0,
            "title":"hello",
            "extract":"Greating to some one"
  }}}}

JSON 2

{ "query":{
      "pages":{
         "2354":{
            "pageid":2354,
            "ns":0,
            "title":"Nice",
            "extract":"Say Appreciation to someone"
  }}}}

在上面 2 JSONs 值正在改变例如 48032354 similary 我不知道在 advacned all值,那么如何为它生成 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 调用但 return 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) {
            }
        });

请告诉我如何解决此类问题。谢谢。

您可以在动态键的情况下使用地图。例如

 @SerializedName("pages")
@Expose
private Map<String, yourmodelclassforinsidedynamickeyvalue> page;

其中 您的模型classforinsidedynamickeyvalue 是您的模型 class for

"pageid":2354,
"ns":0,
"title":"Nice",
"extract":"Say Appreciation to someone"

然后从地图值中你可以得到你的模型class。如果您需要密钥,您可以从地图密钥中获取。

使用动态标签时需要使用 hashMap

喜欢

public class Pages {
    @SerializedName("pageid")
    @Expose
    private Integer pageid;
    @SerializedName("ns")
    @Expose
    private Integer ns;
    @SerializedName("title")
    @Expose
    private String title;
    @SerializedName("extract")
    @Expose
    private String extract;
//....
}

然后在你的回复中使用这个

  if (response.body() != null) {
     Example numberResponse = response.body();
     String keys = numberResponse.getQuery().getPages().toString(); 
     Map<String, Pages> entityList = 
     response.body().getQuery().getPages();
         for (String mapKey : entityList.keySet()) {
         String text = entityList.get(mapKey).getExtract();
            // use text where you want :)
   }}