0

I have an API which returns JSON in below format. Need to design Java class for the same. These values will be varying. So can't create a java class with fixed properties. JSON as:

 {
        "jan": {
            "2020-01-01": {
                "A": 348,
                "B": 408
            },
            "2020-01-02": {
                "A": 348,
                "B": 408   
            }
        },
        "feb": {
            "2020-02-01": {
                "A": 348,
                "B": 408
            },
            "2020-02-02": {
                "A": 348,
                "B": 408
            }  
     
        }       
    }   

How to design Object in Spring Boot/Java for this?

1 Answers1

2

I understand that you are looking for a java class to use to deserialize the JSON structure that you are sharing. This looks to me like

Map<String, Map<String, Data>> 

Where

class Data {
   private int A;
   private int B;
}

Now if you want to deserialize it using for example jackson, you can use

new ObjectMapper().readValue(myJson, new TypeReference<Map<String, Map<String, Data>>>() {});
Juan Rada
  • 3,513
  • 1
  • 26
  • 26