Reputation: 2275
I've been trying to parse the following in my android application but cant figure out how to get that far down the branch. I want to be able to get "description" in "myc" This is what i have:
{
"status": 0,
"result": [
{
"id": 20,
"object_metadata": {
"name": "David",
"myc": [
{
"description": "Hello world"
},
{
"description": "Goodbye World"
}
]
}
}
]
}
Upvotes: 0
Views: 63
Reputation: 440
Try Google GSON. You can make a java class (or a model), implement getters and setters, and easily get and set every value. Parsing JSON via JSONObject
can be frustrating when you are going to manipulate in deeper structure of the JSON specified.
Upvotes: 1
Reputation: 38439
try below code:-
try
{
JSONObject json = new JSONObject("ur json");
String status = json.getString("status");
JSONArray result = json.getJSONArray("result");
for (int i = 0; i < result.length(); i++)
{
JSONObject json_result = result.getJSONObject(i);
String id = json_result.getString("id");
JSONObject object_metadata = json_result.getJSONObject("object_metadata");
String name = object_metadata.getString("name");
JSONArray myc = object_metadata.getJSONArray("myc");
for (int j = 0; j < myc.length(); j++)
{
JSONObject myc_value = myc.getJSONObject(i);
String description = myc_value.getString("description");
}
}
}
catch (Exception e)
{
// TODO: handle exception
e.printStackTrace();
}
Upvotes: 0
Reputation: 3921
JSONObject obj = new JSONObject(jsonstring);
JSONArray array = obj.getJSONArray("result").getJSONObject(0).getJSONObject("object_metadata").getJSONArray("myc");
for (int i = 0; i < array.length(); i++) {
String description = array.getJSONObject(i).getString("description");
}
Upvotes: 1