我如何解析Android中的2个json数组? Plz见下面的代码;
{
"detail": [
{
"price": 51,
"numsc": 2,
"name": "this app is about animals",
"sc1": "printed-dress.jpg",
"sc2": "printed-dress2.jpg"
}
],
"colors": [
{
"color": "#5D9CEC",
"name": "blue"
},
{
"color": "#FCCACD",
"name": "pink"
}
]
}
你能帮帮我吗??答案 0 :(得分:1)
JSONObject object = new JSONObject(your-string);
JSONArray details=object.getJSONArray("details");
for(int j=0;j<details.length();j++){
JSONObject detail= details.getJSONObject(i);
String price = detail.getString("price");
....
}
JSONArray colors = object.getJSONArray("colors");
for(int i=0;i<colors.length();i++){
JSONObject obj= colors.getJSONObject(i);
// parse your json here
String color = obj.getString("color")
}
答案 1 :(得分:0)
请参阅以下代码:
private void decodeJSON()
{
String JSONString = "you json String";
try
{
JSONObject obj = new JSONObject(JSONString);
JSONArray arr = obj.getJSONArray("detail");
JSONObject detail = arr.getJSONObject(0);
int price = detail.getInt("price"); // do same thing to get other values
arr = obj.getJSONArray("colors");
JSONObject color = arr.getJSONObject(0);
String colorValue = color.getString("color");
String name = color.getString("name");
// do same thing for next object in array.
} catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}