JSON到对象映射

时间:2017-09-21 09:28:58

标签: java json jackson

"statValues": {
                                "c__TL_gattooi": {
                                                "value": 90.0
                                },
                                "c_cwc_gattooi": {
                                                "value": 3462.0
                                },
                                "c_gaw__oxcgattooi": {
                                                "value": 11.0
                                },
                                "c_odesb__ox_gattooi": {
                                                "value": 6.0
                                },
                                "c_odesb_cwdc_gattooi": {
                                                "value": 205472.0
                                },
                                "c_ach38_sax_gattooi": {
                                                "value": 1.0
                                },
}

想要将此JSON转换为要与杰克逊一起映射的POJO

1 个答案:

答案 0 :(得分:0)

要从字符串jsonSource反序列化给定的json,请使用类似

的内容
ObjectMapper mapper = new ObjectMapper();
Root root = mapper.readValue(jsonSource, Root.class);

我为你生成了POJO

import java.util.HashMap;

class Root {
    private HashMap<String, Value> statValues = new HashMap<>();

    public HashMap<String, Value> getStatValues() {
        return statValues;
    }

    public void setStatValues(HashMap<String, Value> statValues) {
        this.statValues = statValues;
    }
}

class Value {
    double value;

    public double getValue() {
        return value;
    }

    public void setValue(double value) {
        this.value = value;
    }
}

如果能够提供更好的名称,可以使用@JsonProperty

重新映射属性

要访问价值,请使用root.get("c__TL_gattooi").getValue()之类的内容来获取密钥列表 - root.keys()