JSON in Android - Tutorial
version 1.5, 28.06.2016
JSON with Android. This article describes how to process JSON within Android.
1. Android and JSON
1.1. Included standard library
The Android platform includes the json.org library that allows processing and creating JSON files.
1.2. Example: Reading JSON
Converting a JSON string into a JSON object is also simple. Create the following coding for the activity.
import org.json.JSONArray;
import org.json.JSONObject;
String jsonString = readJsonObjectFromSomeWhere(); (1)
try {
JSONObject json = new JSONObject(jsonString);
} catch (Exception e) {
e.printStackTrace();
}
| 1 | method that provides a JSON string, left out for brevity |
1.3. Write JSON
Writing JSON is simple. Just create the JSONObject or JSONArray and use the toString() method.
public void writeJSON() {
JSONObject object = new JSONObject();
try {
object.put("name", "Jack Hack");
object.put("score", new Integer(200));
object.put("current", new Double(152.32));
object.put("nickname", "Hacker");
} catch (JSONException e) {
e.printStackTrace();
}
System.out.println(object);
}
2. Android JSON links
2.1. vogella Java example code
If you need more assistance we offer Online Training and Onsite training as well as consulting
