Working with JSON - Tuong-Nguyen/Android GitHub Wiki

There is a library in Android for working with JSON: org.json

JSONArray: represent an JSON array object JSONObject: represent an JSON object JSONStringer: like StringBuilder, it is used for building a JSON object JSONTokener: read JSON token from a string

Create an JSON object from string

// JSON array
JSONArray jsonArray = new JSONArray(jsonString);

// JSON object
JSONObject jsonObject = new JSONObject(jsonString);

Build an JSON object:

// JSON array object: add 
JSONArray jsonArray = new JSONArray();
jsonArray.put(5); // add 5 to the end of the array.

// JSON object: add properties
JSONObject jsonObject = new JSONObject();
jsonObject .put("name", "Matt");
jsonObject .put("id", 1);

Convert JSON to string

string jsonString = jsonObject.toString();