【问题标题】:Extract json subset with few attributes from the main json从主 json 中提取具有少量属性的 json 子集
【发布时间】:2016-06-23 20:11:52
【问题描述】:

是否有 API/工具可用于在 java 中提取 json 的特定属性(json 子集),类似于 apache-commons beanutils 副本?

例如我有以下 JSON

{
    "fixed":[
        {
        "b":"some value",
        "c":"some value",
        "d":"some value",
        "e":"some value",
        "f":"some value"
        },
        {
        "b":"value",
        "c":"value",
        "d":"value",
        "e":"value",
        "f":"value"
        }
    ]
}

我想要下面的json

{
    "fixed":[
        {
        "b":"some value",
        "e":"some value",
        "f":"some value"
        },
        {
        "b":"value",
        "e":"value",
        "f":"value"
        }
    ]
}

我想出了以下方法,但不确定它是否正确

public JSONObject parseJSON(JSONObject data,List<String> subset){
        JSONArray fixedArray = (JSONArray) data.get("fixed");
        JSONObject resObj = new JSONObject();
        JSONArray resArray = new JSONArray();
        for(int i=0;i<fixedArray.size();i++){
            JSONObject element = (JSONObject) fixedArray.get(i);
            JSONObject resElement = new JSONObject();
            for(String s:subset){
                resElement.put(s, element.get(s));
            }
            resArray.add(resElement);
        }
        return resObj.put("fixed", resArray);
}

我查看了this SO question,但对这个主题没有帮助。

【问题讨论】:

    标签: java json


    【解决方案1】:

    https://docs.oracle.com/javase/tutorial/jaxb/intro/arch.html 如果你愿意,你也可以从 JAXB 创建你自己的 pojo 类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-20
      • 1970-01-01
      • 1970-01-01
      • 2022-10-08
      • 2022-10-04
      • 2019-10-20
      • 2022-01-12
      相关资源
      最近更新 更多