0

I have a json structure similar to this

{
  "Byc-yes": { // < code
    "Updated": "week", // < period
    "Time": "12pm" // < time
  },
  "Uop-thp":{
    "Updated": "week",
    "Time": "12pm
  } ,
  ...

I want to deserialize it to a Java class

class Updating {
   private String code;
   private String period;
   private String time;
}

There any native JACKSON mappers to do this, or will I need to create my own custom deserializer for this?

c0nf1ck
  • 359
  • 3
  • 14

1 Answers1

2

I will read it as Map.class and then iterate through keyset to extract values.

ObjectMapper objectMapper = new ObjectMapper();
    Map map = objectMapper.readValue(s, Map.class);
    for (Object o : map.keySet()) {
        String key = (String) o;
        System.out.println(key);//prints Byc-yes for first
        Map<String, String> value = (Map<String, String>) map.get(key);
        System.out.println(value); //prints {Updated=week, Time=12pm} for first
        Updating updating = new Updating(key, value.get("Updated"), value.get("Time"));
        System.out.println(updating);
    }

Assuming Updated and Time are fixed keys.

Mritunjay
  • 25,338
  • 7
  • 55
  • 68