-3

我有下面的 json 来填充 recyclerview 项目

{
"success": true,
"data": {
    "houses": [
        {
            "id": 1,
            "house_id": "HOUSEAGA00001",
            "ward_number": "23",
            "staus_id":1,
            "name_of_resident": "Melroy",
            "phone_number": "9890098900",
            "amount_due": 5000,
            "created_date": "2018-10-12T18:30:00.000Z",
            "updated_date": "2018-10-12T18:30:00.000Z"
        },
        {
            "id": 2,
            "house_id": "HOUSEAGA00002",
            "ward_number": "24",
            "staus_id":1,
            "name_of_resident": "Prajyot",
            "phone_number": "9823598235",
            "amount_due": 10000,
            "created_date": "2018-10-12T18:30:00.000Z",
            "updated_date": "2018-10-12T18:30:00.000Z"
        }
    ],
    "payment_statuses": [
        {
            "id": 1,
            "type": "Collected",
            "color_hex": "#00ff00"
        },
        {
            "id": 3,
            "type": "Owner unavailable",
            "color_hex": "#ff9900"
        },
        {
            "id": 2,
            "type": "Owner Denied",
            "color_hex": "#ff0000"
        }
    ]
},
"count": 2,
"error": []

}

我正在使用 house 数组来填充 recycler 视图项目,但是项目中有一个名为 status 的字段,我必须从 house 数组中获取状态 ID,并从 payment_statuses 数组中获取其各自的类型。问题是我如何从回收站视图持有者中的房屋数组中获取状态支付状态数组中的类型。

4

1 回答 1

0

我认为这可能会有所帮助:How parse JSON in Android 特别假设您在以下位置拥有整个 JSON 对象object

要获取特定的JSONArray

JSONObject jObject = object.getJSONObject("data"); 
JSONArray jArray = jObject.getJSONArray("houses");

从数组中获取项目

for (int i=0; i < jArray.length(); i++)
{
    try {
        JSONObject oneObject = jArray.getJSONObject(i);
        // Pulling items from the array
        int value = oneObject.getInt(
    } catch (JSONException e) {
        // Oops
    }
}

我没有测试代码,但想法是这样的。

于 2018-10-19T05:59:52.537 回答