如何用杰克逊将这个json转换成pojo?

时间:2018-03-14 11:07:32

标签: json jackson

{
    "code": 200,
    "status": "OK",
    "developerMessage": "OK",
    "userMessage": "Operation Successful",
    "data": {
        "settings": {
            "countries": {
                "1": "Afghanistan",
                "2": "Albania",
                "3": "Algeria",
                "4": "Andorra",
                "5": "Angola",

            },
            "mobile-code": {
                "+93": "Afghanistan +93",
                "+355": "Albania +355",
                "+213": "Algeria +213",
                "+376": "Andorra +376",
                "+244": "Angola +244",
            }


        },
        "status_code": 200,
        "success": true,
    },
    "dataType": "map"
}

2 个答案:

答案 0 :(得分:0)

  

这些是数组,但表示为键值。

您可以考虑Map<String, Object>然后:

ObjectMapper mapper = new ObjectMapper();

TypeReference<HashMap<String, Object>> typeReference = 
        new TypeReference<HashMap<String, Object>>() {};
Map<String, Object> data = mapper.readValue(json, typeReference);

答案 1 :(得分:0)

我得到了这样的话:

 @JsonIgnoreProperties(ignoreUnknown = true)
    public class Settings {
        @SerializedName("countries")
        Map<String, String> countries;

        public Map<String, String> getCountries() {
            return countries;
        }

        public void setCountries(Map<String, String> countries) {
            this.countries = countries;
        }
    }