9

I want to convert from JSONObject

{"CNo":80,"CName":"ganesh","CMail":"ganesh@ganesh.com","CMailType":"home","CPhNo":9878987776,"CPhNoType":"home","ClientNo":1}

to

{\"CNo\":80,\"CName\":\"ganesh\",\"CMail\":\"ganesh@ganesh.com\",\"CMailType\":\"home\",\"CPhNo\":9878987776,\"CPhNoType\":\"home\",\"ClientNo\":1}

Gromer
  • 9,861
  • 4
  • 34
  • 55
the_dopamine
  • 899
  • 6
  • 12
  • 21
  • Do you have a json-formatted *string* and you want to escape the `"` using backslash in this string? Or do you start with a JSONObject, and want to convert it to a string? – aioobe Oct 29 '10 at 06:57
  • yeah, I suggest you clarify your question a bit. And use code formatting, please. – theTuxRacer Oct 29 '10 at 07:04
  • hi aioobe i just need the below format from a existing json object.. – the_dopamine Oct 29 '10 at 07:19

3 Answers3

11

Try to use toString() on your JSON object

Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
  • ya i checked.. its the same object which is returning.. to the stringentity.. !! im assigning the json.tostring() to a stringentity variable so that i can use this to post to a .net rest client – the_dopamine Oct 29 '10 at 08:25
6

I hope this helps. Just learnt it yesterday :)

JSONObject foo = yourJSONOject;
String uid = foo.get("CNo").isString().toString();
String type = foo.get("CName").isString().toString();
.
. //for each Key field.

I am not sure why you have put the escapes in the string, but you can call append() and get the OP as you want it.

jnthnjns
  • 8,962
  • 4
  • 42
  • 65
theTuxRacer
  • 13,709
  • 7
  • 42
  • 59
2

Below code will convert the given JsonObject to string.

Gson gson = new Gson();
String bodyInStringFormat = gson.toJson(passYourJsonObjectHere);
syed dastagir
  • 419
  • 6
  • 8