JSON.stringify in java - android

בועז יערי picture בועז יערי · Feb 27, 2014 · Viewed 97.4k times · Source

Is there any way to perform a JSON.stringify in android?

I keep seeing JSON.stringify(JSONObject) all around the web, but I cant find the JSON class in android.

Any help?

Answer

MH. picture MH. · Feb 27, 2014

JSON.stringify(JSONObject) is a Javascript function and will not be available in Java. If you're using the org.json.* package built in the Android SDK, the equivalent would be to simply call toString() on your JSONObject instance, or the more human-friendly toString(int).

http://developer.android.com/reference/org/json/JSONObject.html#toString() http://developer.android.com/reference/org/json/JSONObject.html#toString(int)

JSONObject obj = ...
String jsonString = obj.toString(4);