在Android中使用Volley库解析嵌套的JSON数组

时间:2019-09-26 13:33:14

标签: android json android-volley

如何使用Volley库解析嵌套的JSON数组? 我的JSON数据结构屏幕截图。 https://prnt.sc/pbaea5

我需要分析分数值。

JSONArray jsonArray = response.getJSONArray("matches");

    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject jsonObjectMatchs = jsonArray.getJSONObject(i);

        // bat_team node is JSON Object
        JSONObject bat_teamData = jsonObjectMatchs.getJSONObject("bat_team");
        JSONArray jsonArrayInnings = bat_teamData.getJSONArray("innings");
        JSONObject jsonObjectInnings = jsonArrayInnings.getJSONObject(i);


        String bat_team_score = jsonObjectInnings.getString("score");
    }

1 个答案:

答案 0 :(得分:0)

您需要进一步迭代 const colors = { 1: "blue", 2: "red", 3: "green" } const shirts = [ {id: 1, color: 2}, {id: 2, color: 3}, {id: 3, color: 2}, {id: 4, color: 1}, {id: 5, color: 3} ]; shirts.forEach( shirt => shirt.colorName = colors[shirt.color] ) console.log(shirts)使用另一个循环,但是在您的代码中,您正在使用父循环的const colors = [ {id: 1, name: "blue"}, {id: 2, name: "red"}, {id: 3, name: "green"} ]; const shirts = [ {id: 1, color: 2}, {id: 2, color: 3}, {id: 3, color: 2}, {id: 4, color: 1}, {id: 5, color: 3} ]; const colorCodes = colors.reduce((o, c) => { o[c.id] = c.name; return o }, {}) shirts.forEach( shirt => shirt.colorName = colorCodes[shirt.color] ) console.log(shirts)。 它将无法正常工作。