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}
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.
// 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);
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.
Below code will convert the given JsonObject to string.
Gson gson = new Gson();
String bodyInStringFormat = gson.toJson(passYourJsonObjectHere);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With