从主JSON中提取JSON子集的几个属性



是否有可用于在Java中提取JSON的特定属性(JSON子集)的API/工具,类似于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);
}

我看了这个问题,但对这个话题没有帮助。

https://docs.oracle.com/javase/tutorial/jaxb/intro/arch.html您还可以从Jaxb创建自己的POJO类,如果您想要。

最新更新