9

I want to convert from JSONObject

{"CNo":80,"CName":"ganesh","CMail":"[email protected]","CMailType":"home","CPhNo":9878987776,"CPhNoType":"home","ClientNo":1}

to

{\"CNo\":80,\"CName\":\"ganesh\",\"CMail\":\"[email protected]\",\"CMailType\":\"home\",\"CPhNo\":9878987776,\"CPhNoType\":\"home\",\"ClientNo\":1}

3
  • 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? Commented Oct 29, 2010 at 6:57
  • yeah, I suggest you clarify your question a bit. And use code formatting, please. Commented Oct 29, 2010 at 7:04
  • hi aioobe i just need the below format from a existing json object.. Commented Oct 29, 2010 at 7:19

3 Answers 3

11

Try to use toString() on your JSON object

Sign up to request clarification or add additional context in comments.

1 Comment

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
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.

2 Comments

do we have anything like javascriptserializer as we use in .net?
im sorry, im not familiar with .net. you can define a "\" and append it/insert it where you require.
2

Below code will convert the given JsonObject to string.

Gson gson = new Gson();
String bodyInStringFormat = gson.toJson(passYourJsonObjectHere);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.