java - How to represent key value in a JSON string in predefined format? -
i have json string in have key , value shown below -
{ "u":{ "string":"1235" }, "p":"2047935", "client_id":{ "string":"5" }, "origin":null, "item_condition":null, "country_id":{ "int":3 }, "timestamp":{ "long":1417823759555 }, "impression_id":{ "string":"2345hh*" }, "is_consumerid":true, "is_pid":false }
as example, 1 key "u"
, value -
{ "string":"1235" }
similarly key "country_id"
, value -
{ "int":3 }
similarly others well. need is, need represent above key value shown below. if value string data type, represent in double quotes, otherwise don't represent in double quotes
"u": "1235" "p": "2047935" "client_id": "5" "origin":null "item_condition":null "country_id": 3 // don't have double quotes here around 3 since country_id int that's why "timestamp": 1417823759555 "impression_id": "2345hh*" "is_consumerid": true "is_pid": false
and need make json string should -
{ "u": "1235", "p": "2047935", "client_id": "5", "origin":null, "item_condition":null, "country_id": 3, "timestamp": 1417823759555, "impression_id": "2345hh*", "is_consumerid": true, "is_pid": false }
what best , efficient way this?
update:-
this have got -
jsonobject jsonobject = new jsonparser().parse(response).getasjsonobject(); (map.entry<string, jsonelement> object : jsonobject.entryset()) { if (object.getvalue() instanceof jsonobject) { string data = object.getvalue().tostring(); map<string, object> jsonin = gson.fromjson(data, type); map<string, object> jsonout = new hashmap<string, object>(); set<string> keys = jsonin.keyset(); (string key : keys) { object value = jsonin.get(key); if (value instanceof map) { map<?, ?> mapvalue = (map<?, ?>) value; (map.entry<?, ?> entry : mapvalue.entryset()) { jsonout.put(key, entry.getvalue()); } } else { jsonout.put(key, value); } } // system.out.println(jsonout); type typeofmap = new typetoken<map<string, string>>() { }.gettype(); string json = gson.tojson(jsonout, typeofmap); system.out.println(json); } }
only thing not working - when try serialize jsonout map json, values of particular key gets converted string. need is, values should converted string (double quotes) in general country_id
value should not in double quotes since integer per original json.
pseudocode:
map<object> jsonin = jsonparser.parsejsontomap(thejson); map<object> jsonout = new hashmap<object>(); string[] keys = jsonin.getkeys(); (string key in keys) { object value = jsonin.get(key); if (value instanceof map) { map mapvalue = (map) value; object[] valuevalues = mapvalue.getvalues(); assert valuevalues.length == 1; jsonout.add(key, valuevalues[0]); } else { jsonout.add(key, value); } }
this code cheats little relying on fact 1 shouldn't need manipulate data types, since they're correct. 1 add more logic inspect key values of inner maps determine data types, see no point, unless sort of data type translation needed.
some json kits have own classes, such jsonobject, use instead of maps, they're subclasses of map , logic same (though instanceof
replaced querying type of value, , (map)
cast replaced specific getobject
method call or such.
Comments
Post a Comment