Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert json object to string in android..?

Tags:

json

android

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}

like image 857
the_dopamine Avatar asked Oct 29 '10 06:10

the_dopamine


People also ask

How do I convert a JSON object to a String?

Use the JavaScript function JSON.stringify() to convert it into a string. const myJSON = JSON.stringify(obj); The result will be a string following the JSON notation.

How do you pass a JSON object into a String in Java?

// creating object of Gson Gson gson = new Gson(); // calling method fromJson and passing JSON string into object // The first parameter is JSON string // The second parameter is the Java class to parse the JSON into an instance of. object = gson. fromJson(jsonString,GFG. class);

What is JSON object in Android?

JSON (JavaScript Object Notation) is a straightforward data exchange format to interchange the server's data, and it is a better alternative for XML. This is because JSON is a lightweight and structured language.


2 Answers

Below code will convert the given JsonObject to string.

Gson gson = new Gson();
String bodyInStringFormat = gson.toJson(passYourJsonObjectHere);
like image 161
syed dastagir Avatar answered Sep 21 '22 21:09

syed dastagir


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.

like image 34
theTuxRacer Avatar answered Sep 23 '22 21:09

theTuxRacer