Decoding JSON data
suggest changeIf you need to get data from a JSONObject
, consider the following example:
String json = "{\"foo\":\"bar\",\"temperature\":21.5,\"year\":2016,\"message\":{\"Hello\":\"world\"},\"months\":[\"January\",\"February\",\"March\",null,\"May\"]}"; // Decode the JSON-encoded string JSONObject object = new JSONObject(json); // Retrieve some values String foo = object.getString("foo"); double temperature = object.getDouble("temperature"); int year = object.getInt("year"); // Retrieve another object JSONObject secondary = object.getJSONObject("message"); String world = secondary.getString("Hello"); // Retrieve an array JSONArray someMonths = object.getJSONArray("months"); // Get some values from the array int nMonths = someMonths.length(); String february = someMonths.getString(1);
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents