When i create a json object and add it to json array, it puts extra backslashes :/
1) Creating JSONObject
JSONObject jo = new JSONObject();
jo.put("JobName", "Test - Job Name");
jo.put("JobStatus", "Current");
jo.put("OrganID", "123");
jo.put("Date_Entered", getDate());
Result:
{"OrganID":"123","JobName":"Test - Job Name","Date_Entered":"13-Apr-2015","JobStatus":"Current"}
2) Adding JSONObject to JSONArray
JSONArray ja = new JSONArray();
ja.put(jo);
Result (It also puts extra double quotes " before and after JSONObject):
["{\"OrganID\":\"123\",\"JobName\":\"Test - Job Name\",\"Date_Entered\":\"13-Apr-2015\",\"JobStatus\":\"Current\"}"]
3) Adding JSONArray to JSONObject
JSONObject finalJson = new JSONObject();
finalJson.put("PostCompJob", ja.toString());
Result:
{"PostCompJob":"[\"{\\\"OrganID\\\":\\\"123\\\",\\\"JobName\\\":\\\"Test - Job Name\\\",\\\"Date_Entered\\\":\\\"13-Apr-2015\\\",\\\"JobStatus\\\":\\\"Current\\\"}\"]"}
I dont know why is this happening, can anyone please help me out?
1 Answer 1
Oh Gosshh I have got the same issue But on my side it was due to AWS library. As I was using this Lib in my project and it automatically imports JSONObject or JSONArray of AWS lib whenever I have created this object. Slashes only created when I have created Json of AWS. If you are using some other library Please check first.
System.out.println(ja);I don't get any backslashes. Step 3 certainly would add backslashes, because you're trying to represent JSON as a string value in JSON.