Asked
Modified
11 years, 8 months ago
Viewed
71 times
Part
of PHP and Mobile Development Collectives
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"
}
]
}
}
]
}
Hariharan
24.9k6 gold badges52 silver badges56 bronze badges
asked May 2, 2014 at 3:37
user2423476
2,2859 gold badges29 silver badges41 bronze badges
-
Can you post your java code as well ?Ye Lin Aung– Ye Lin Aung2014年05月02日 03:46:27 +00:00Commented May 2, 2014 at 3:46
3 Answers 3
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");
}
answered May 2, 2014 at 3:53
henry4343
3,9315 gold badges25 silver badges32 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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.
Comments
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();
}
answered May 2, 2014 at 3:56
duggu
38.5k12 gold badges121 silver badges114 bronze badges
Comments
lang-php