java - 如何使用动态键为 json 创建 POJO 类以进行改造?

标签 java android json gson retrofit2

{
  "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。

拜托,拜托,请帮助我。 我对此进行了很多搜索,但没有任何有效的方法。 另外我是 retrofit 新手,请详细回答。 我看到一些答案提到在这种情况下使用 map (例如 Parse Dynamic Key Json String using Retrofit )。但这些答案没有得到适当的阐述。请帮我彻底理解它。

最佳答案

您可以在 Pages 类中使用 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);
}

关于java - 如何使用动态键为 json 创建 POJO 类以进行改造?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44844226/

相关文章:

Android CountDownTimer 和 AlertDialog

ios - UISegmentedControl filter Table View data JSON 错误

java - 如何在不知道其真实类型的情况下比较两个对象

android - 从 android 选项菜单启动 Activity

java - 模拟 Spring 中许多类中常见的类实例

java - Async Task 是否有某种形式的缓存系统?

php - 使用PHP将多个数组数据中的JSON插入到MySQL中

javascript - 从 DOM 和 JQuery 创建 JSON 数组

java - 修改 JTable 中的整个列

java - JTable 中的 JComboBox 不显示选择