javaandroidjsonjacksongoogle-http-client

Proper POJO for serializing JSON with Jackson library


So the question is how to properly build POJO for JSON that looks like this:

{"1":{
       "value_1":"something",
       "value_2":"something else"},
 "2": {
       "value_1":"something",
       "value_2":"something else"},
 "3": {
       "value_1":"something",
       "value_2":"something else"},

  ...

 "999": {
         "value_1":"something",
         "value_2":"something else"}}

The problem is that I cant parse JSON as shown in Google Developers docs because there is to many different names to an array that holds the data I need.


Solution

  • You could try it to parse like this(spoiler a lot of code)

    public class ParseTo {
    
        private Map<String, InnerParse> unknown = new HashMap<>();
    
        @JsonAnyGetter
        public Map<String, InnerParse> get(){
            return unknown;
        }
    
        @JsonAnySetter
        public void set(String key, Object value){
            if(value instanceof Map){
                Map<String, String> m = (Map<String, String>) value;
                unknown.put(key, new InnerParse(m));
            }
        }
    }
    
    public class InnerParse {
    
        private static final String V1_PROPERTY = "value_1";
        private static final String V2_PROPERTY = "value_2";
    
        public InnerParse(Map<String, String> map){
            this.value1 = map.get(V1_PROPERTY);
            this.value2 = map.get(V2_PROPERTY);
        }
    
        private String value1;
        private String value2;
    
        public String getValue1() {
            return value1;
        }
    
        public void setValue1(String value1) {
            this.value1 = value1;
        }
    
        public String getValue2() {
            return value2;
        }
    
        public void setValue2(String value2) {
            this.value2 = value2;
        }
    }
    
    public class Main {
    
        public static void main(String[] args) {
            String jsonString = "{\"1\":{\n" +
                "       \"value_1\":\"something\",\n" +
                "       \"value_2\":\"something else\"},\n" +
                " \"2\": {\n" +
                "       \"value_1\":\"something\",\n" +
                "       \"value_2\":\"something else\"}" +
                "}";
            try {
                ParseTo parsed = new ObjectMapper().readValue(jsonString, ParseTo.class);
    
                parsed.get().entrySet().stream()
                    .forEach(entry -> {
                        System.out.println("key: " + entry.getKey() + ", value: (" +
                            " Inner value1: " + entry.getValue().getValue1() + "," +
                            " Inner value2: " + entry.getValue().getValue2() +
                            ")");
                    });
    
            } catch (Exception e){
                e.printStackTrace();
            }
        }
    }
    

    dependencies :

    com.fasterxml.jackson.core jackson-core

    com.fasterxml.jackson.core jackson-annotations

    com.fasterxml.jackson.core jackson-databind