Android - JsonArray上的循环?

时间:2016-08-03 06:23:45

标签: java android android-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
}]

3 个答案:

答案 0 :(得分:3)

您的回答应该是这样的回答看起来像我的回答

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

答案 1 :(得分:1)

使用多循环概念

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

答案 2 :(得分:1)

你发布的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
}]