i want convert hashmap json string send on wire. here code
public static void main(string []args) throws jsonexception, jsonparseexception, jsonmappingexception, ioexception { map<string,string> varmap = new mymap<string,string>(); varmap.put("var","123"); varmap.put("other_var","234"); gson gson = new gsonbuilder().create(); string jsonstring = gson.tojson(varmap); jsonobject json = new jsonobject(); json.put("variable",jsonstring); system.out.println("json " + json); }
the output
json {"variable":"{\"var\":\"123\",\"other_var\":\"234\"}"}
which assume right thing, examples i've found dont include backslash in strng. understand backslash escapes double quotes , hence none of string replace method work replace backslash.
is there trick following output?
json {"variable":{"var":"123","other_var":"234"}}
can custom serialization property used somehow not add backslashes?
this should give result want. you're json-encoding json-string , adding backslashes escape characters.
public static void main(string []args) throws jsonexception, jsonparseexception, jsonmappingexception, ioexception{ map<string, object> top = new hashmap<string, object>(); map<string, object> vars = new hashmap<string, object>(); vars.put("var","123"); vars.put("other_var","234"); top.put("variable", vars); gson gson = new gsonbuilder().create(); string jsonstring = gson.tojson(top); system.out.println("json " + json); }
Comments
Post a Comment