java - Convert JSON Object as a Flat Obejct with Key value pair -


i getting json object looks like:

{     "id": "1",     "name": "hw",     "price": {         "value": "10"     },     {         "items": [{             "id": "1"         }]     } } 

i want represent flat map, want represent array of items list.

my output should like:

{ "id": "1", "name":"hw", "price":"10", "items": ["1"] } 

can suggest me how can achieve this? tried approach:

how deserialize json flat, map-like structure?

output above tried link:

{ "id": "1", "name":"hw", "price.value":"10", "items[0].id": "1" } 

but representing arrays values array[0], array[1] don't need. need array list.

the json you've given not valid. assume it's:

{     "id": "1",     "name": "hw",     "price": {         "value": "10"     },     "items": [{         "id": "1"     }] } 

there cannot generic solution you're asking. particular json, do(using json-simple):

@suppresswarnings("unchecked") public map<string, string> transform(string inputjson) throws parseexception {     map<string, string> result = new linkedhashmap<>();     jsonobject inputjsonobj = (jsonobject) new jsonparser().parse(inputjson);     string id = inputjsonobj.getordefault("id", "").tostring();     string name = inputjsonobj.getordefault("name", "").tostring();     string price = ((jsonobject) inputjsonobj.getordefault("price", new jsonobject())).getordefault("value", "")             .tostring();     jsonarray itemsarray = (jsonarray) inputjsonobj.getordefault("items", new jsonarray());     int n = itemsarray.size();     string[] itemids = new string[n];     (int = 0; < n; i++) {         jsonobject itemobj = (jsonobject) itemsarray.get(i);         string itemid = itemobj.getordefault("id", "").tostring();         itemids[i] = itemid;     }     result.put("id", id);     result.put("name", name);     result.put("price", price);     result.put("items", arrays.tostring(itemids));     return result; } 

Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -