2

How I can loop on this 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
}]
6
  • 1
    Follow this Commented Aug 3, 2016 at 6:26
  • this is not even a valid json format.It has duplicate keys name. Commented Aug 3, 2016 at 6:27
  • Use multi looping concept given below Commented Aug 3, 2016 at 6:29
  • @sinsuren.the json is true. Commented Aug 3, 2016 at 6:31
  • 1
    @JoJoRoid Try My answer You need to Response like my answer and you get easily your values also code check Commented Aug 3, 2016 at 6:43

3 Answers 3

4

Your Response Should be like that Have A look to response is like my answer

[{
    "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

}]

Code to Take Value

     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();
    }   
Sign up to request clarification or add additional context in comments.

Comments

2

Use multi looping concept

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

Comments

1

there is a mistake in your posted json...

this should be the right 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
}]

1 Comment

Some formatting would be nice to help spot the mistake

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.