java - How to serialize json to another json by preserving data types of key? -


i have original 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 } 

now need is, need represent key value pair shown below. if value string data type (like value key u), represent it's value in double quotes, otherwise don't represent it's value in double quotes. meaning value of country_id won't in string double quotes since int.

"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 } 

so started below code not able understand should further?

    string response = "original_json_string";     type type = new typetoken<map<string, object>>() {}.gettype();      jsonobject jsonobject = new jsonparser().parse(response).getasjsonobject();      (map.entry<string, jsonelement> object : jsonobject.entryset()) {         if (object.getvalue() instanceof jsonobject) {             string data = object.getvalue().tostring();             // not sure should here?          }     } 

and new json should print out after serializing.

{     "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 way achieve this?

note i'm not yet experienced gson, there might easiest ways it. solution comes after discussion had previously.

basically problem wanted type in json file (which done addentry method) , each @event key should have own json string (done computejson). since there 2 nested levels, it's fine that. otherwise recursive approach trick.

so if have 1 nested level, should iterate other jsonobject's entries'. each entries, computejson add new json entry in list corresponds each @event key.

public class test {     public static void main(string[] args) throws exception {            list<string> output = new arraylist<>();         jsonobject jsonobject = new jsonparser().parse(new filereader("myjson.json")).getasjsonobject();         (map.entry<string, jsonelement> object : jsonobject.entryset()) {             if (object.getvalue() instanceof jsonobject) {                 output.add(computejson((jsonobject)object.getvalue()));             }         }         system.out.println(output);     }      private static string computejson(jsonobject source) {         jsonobject output = new jsonobject();         (map.entry<string, jsonelement> object : source.entryset()) {             if (object.getvalue() instanceof jsonobject) {                 for(map.entry<string, jsonelement> entry : ((jsonobject)object.getvalue()).entryset()) {                     addentry(object.getkey(), output, entry);                 }             } else {                 addentry(object.getkey(), output, object);             }         }         gson gson = new gsonbuilder().serializenulls().setprettyprinting().create();         return gson.tojson(output);     }      private static void addentry(string key, jsonobject output, map.entry<string, jsonelement> object) {         switch(object.getkey().tolowercase()) {             case "string":                 output.addproperty(key, object.getvalue().getasstring());                 break;             case "int":                 output.addproperty(key, object.getvalue().getasint());                 break;             case "long":                 output.addproperty(key, object.getvalue().getaslong());                 break;             //add other primitive cases             default:                 output.add(key, object.getvalue());         }     } } 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -