Android - 在 JsonArray 上循环?

Android - Loop on JsonArray?

我如何在这个 JSON 上循环:

[{
    "X": "54.6000621",
    "Y": "45.8360411",
    "Dates": [{
        "Date": "2000\/04\/26",
        "Time": "12:13:45"
    }, {
        "Date": "2000\/04\/26",
        "Time": "13:13:45"
    }, {
        "Date": "2000\/04\/26",
        "Time": "14:13:12"
    }, {
        "Date": "2000\/04\/26",
        "Time": "15:13:10"
    }, {
        "Date": "2000\/04\/26",
        "Time": "16:13:48"
    }],
    "Count": 5,
    "X": "98.6254621",
    "Y": "99.8360411",
    "Dates": [{
        "Date": "2012\/04\/26",
        "Time": "12:13:45"
    }, {
        "Date": "2012\/04\/26",
        "Time": "13:13:45"
    }, {
        "Date": "2012\/04\/26",
        "Time": "14:13:12"
    }, {
        "Date": "2012\/04\/26",
        "Time": "15:13:10"
    }, {
        "Date": "2012\/04\/26",
        "Time": "16:13:48"
    }, {
        "Date": "2012\/04\/26",
        "Time": "15:13:10"
    }, {
        "Date": "2012\/04\/26",
        "Time": "15:13:10"
    }],
    "Count": 7,
    "X": "58.4582621",
    "Y": "85.8360411",
    "Dates": [],
    "Count": 0
}]

使用多循环概念

try {
            JSONArray mArray = new JSONArray("your resonce json string");
            for (int index = 0; index < mArray.length(); index++) {
                JSONObject mJsonObject = mArray.getJSONObject(0);
                JSONArray mDataArray = mJsonObject.getJSONArray("Dates");
                for (int row = 0; row < mDataArray.length(); row++) {
                    JSONObject mDatesObject = mDataArray.getJSONObject(0);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

您发布的内容有误 json...

这应该是正确的json...

[{
    "X": "54.6000621",
    "Y": "45.8360411",
    "Dates": [{
        "Date": "2000\/04\/26",
        "Time": "12:13:45"
    }, {
        "Date": "2000\/04\/26",
        "Time": "13:13:45"
    }, {
        "Date": "2000\/04\/26",
        "Time": "14:13:12"
    }, {
        "Date": "2000\/04\/26",
        "Time": "15:13:10"
    }, {
        "Date": "2000\/04\/26",
        "Time": "16:13:48"
    }],
    "Count": 5,
    "X": "98.6254621",
    "Y": "99.8360411",
    "Dates": [{
        "Date": "2012\/04\/26",
        "Time": "12:13:45"
    }, {
        "Date": "2012\/04\/26",
        "Time": "13:13:45"
    }, {
        "Date": "2012\/04\/26",
        "Time": "14:13:12"
    }, {
        "Date": "2012\/04\/26",
        "Time": "15:13:10"
    }, {
        "Date": "2012\/04\/26",
        "Time": "16:13:48"
    }, {
        "Date": "2012\/04\/26",
        "Time": "15:13:10"
    }, {
        "Date": "2012\/04\/26",
        "Time": "15:13:10"
    }],
    "Count": 7,
    "X": "58.4582621",
    "Y": "85.8360411",
    "Dates": [],
    "Count": 0
}]

你的回复应该是这样的 看看回复就像我的回答一样

[{
    "X": "54.6000621",
    "Y": "45.8360411",
    "Dates": [{
    "Date": "2000\/04\/26",
    "Time": "12:13:45"
    }, {
    "Date": "2000\/04\/26",
    "Time": "13:13:45"
    }, {
    "Date": "2000\/04\/26",
    "Time": "14:13:12"
    }, {
    "Date": "2000\/04\/26",
    "Time": "15:13:10"
    }, {
    "Date": "2000\/04\/26",
    "Time": "16:13:48"
    }],
    "Count": 5
},
{
    "X": "98.6254621",
    "Y": "99.8360411",
    "Dates": [{
    "Date": "2012\/04\/26",
    "Time": "12:13:45"
    }, {
    "Date": "2012\/04\/26",
    "Time": "13:13:45"
    }, {
    "Date": "2012\/04\/26",
    "Time": "14:13:12"
    }, {
    "Date": "2012\/04\/26",
    "Time": "15:13:10"
    }, {
    "Date": "2012\/04\/26",
    "Time": "16:13:48"
    }, {
    "Date": "2012\/04\/26",
    "Time": "15:13:10"
    }, {
    "Date": "2012\/04\/26",
    "Time": "15:13:10"
    }],
    "Count": 7
},
{
    "X": "58.4582621",
    "Y": "85.8360411",
    "Dates": [],
    "Count": 0

}]

取值代码

     try {
        JSONArray jsonArray=new JSONArray(response);
        for (int i=0;i<jsonArray.length();i++)
        {
            JSONObject jsonObject=jsonArray.getJSONObject(i);

            String X=jsonObject.getString("X");
            String Y=jsonObject.getString("Y");
            String Count=jsonObject.getString("Count");

            JSONArray jsonArraydates=jsonObject.getJSONArray("Dates");

            for (int j=0;j<jsonArraydates.length();j++)
            {
                JSONObject jsonObjectDates=jsonArraydates.getJSONObject(j);


                String Date=jsonObjectDates.getString("Date");
                String Time=jsonObjectDates.getString("Time");


            }

        }
    } catch (JSONException e) {
        e.printStackTrace();
    }