java - How to translate the Json in to POJO -


i have following json need able present in pojo, how ?

the json has 1 key per day , value each key 2 separate logical arrays, using jackson library.

{   "2014/01/02": [     {"abc": 2.25, "xyz": 4.05},     {"amazon.com": 3}   ],   "2014/01/03": [     {"abc": 13.02},     {"amazon.com": 3}   ] } 

you don't have option. that's ugly looking json. there no consistent format, can create pojo.

things need consider. {} json object. im java mapping terms, can either map pojo, or map. in order map pojo, need common property names , formatting, of doesn't seem have. seems varying. may need use map. here's how @ it

{  // start map   "2014/01/02": [ // map key: start array ( list<map> )     {"abc": 2.25, "xyz": 4.05},  // start map     {"amazon.com": 3}  // map 

you can use @jsonanysetter simplify, using pojo wrapper, still have, in end map<string, list<map<string, object>> pointed out in comment. if want pojos, there needs common formatting.

here's example of 1 way make work

pojo

public class dynamic {     private map<string, object> map = new hashmap<>();      public object get(string key) {         return map.get(key);     }      public map<string, object> getmap() {         return map;     }      @jsonanysetter     public void set(string name, object value) {         map.put(name, value);     } } 

test (using exact json in simple.json file)

public class test {      public static void main(string[] args) throws exception {         objectmapper mapper = new objectmapper();         dynamic dynamic = mapper.readvalue(new file("simple.json"), dynamic.class);         (string key : dynamic.getmap().keyset()) {             system.out.println("key: " + key);             system.out.println("--------");             list<map<string, object>> list = (list<map<string, object>>) dynamic.get(key);             (map<string, object> map : list) {                 (map.entry<string, object> entry: map.entryset()) {                     system.out.println("key: " + entry.getkey() + ", value: " + entry.getvalue());                 }             }             system.out.println("========");         }     } } 

result

key: 2014/01/03 -------- key: abc, value: 13.02 key: amazon.com, value: 3 ======== key: 2014/01/02 -------- key: abc, value: 2.25 key: xyz, value: 4.05 key: amazon.com, value: 3 ======== 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -