Android: Parse 2 jsonArray [closed]

2019-09-27 10:17发布

How i can parse 2 json Array in Android? Plz see the following code;

    {
"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"
    }
]

}

can you help me plz??

标签: android json
2条回答
做自己的国王
2楼-- · 2019-09-27 10:44
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")

    }
查看更多
我命由我不由天
3楼-- · 2019-09-27 11:09

See following code:

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();
        }

    }
查看更多
登录 后发表回答