0
["[{\"timeFrom\":\"06:00:00\",\"timeTo\":\"09:00:00\",\"title\":\"First\"},{\"timeFrom\":\"09:00:00\",\"timeTo\":\"12:00:00\",\"title\":\"Second\"},{\"timeFrom\":\"12:00:00\",\"timeTo\":\"16:00:00\",\"title\":\"Third\"},{\"timeFrom\":\"16:00:00\",\"timeTo\":\"20:00:00\",\"title\":\"Fourth\"},{\"timeFrom\":\"20:00:00\",\"timeTo\":\"21:30:00\",\"title\":\"5th\"},{\"timeFrom\":\"21:30:00\",\"timeTo\":\"00:00:00\",\"title\":\"Dessert (within two hours of bedtime)th\"}]"]

I got this array when I log JSONArray. How can I iterate over it and get those values ?

edit

for (int i = 0; i < MyService.Meals.length(); ++i) { 
JSONObject rec = MyService.Meals.getJSONObject(i); String text = rec.getString("timeFrom"); 
}

Doesn't return anything because MyService.Meals.length() = 1.

MyService.Meals is JSONArray instance

9
  • possible duplicate of JSON Array iteration in Android/Java Commented Nov 14, 2013 at 21:16
  • Try the examples on this page? stackoverflow.com/questions/3408985/… Commented Nov 14, 2013 at 21:17
  • none of this examples worked, I have already tried it ... Commented Nov 14, 2013 at 21:20
  • Your JSON string looks malformed, the outer square brackets are surrounding one string, so of course the length of the array is one. Commented Nov 14, 2013 at 21:25
  • 1
    Remove the [" and "] from the outside of the string, and then remove all the `` in front of the quotes, and see what happens then Commented Nov 14, 2013 at 21:27

2 Answers 2

1

The issue arises because the format of your array is as follows:

[
  "[{\"timeFrom\":\"06:00:00\",\"timeTo\":\"09:00:00\",\"title\":\"First\"},{\"timeFrom\":\"09:00:00\",\"timeTo\":\"12:00:00\",\"title\":\"Second\"},{\"timeFrom\":\"12:00:00\",\"timeTo\":\"16:00:00\",\"title\":\"Third\"},{\"timeFrom\":\"16:00:00\",\"timeTo\":\"20:00:00\",\"title\":\"Fourth\"},{\"timeFrom\":\"20:00:00\",\"timeTo\":\"21:30:00\",\"title\":\"5th\"},{\"timeFrom\":\"21:30:00\",\"timeTo\":\"00:00:00\",\"title\":\"Dessert (within two hours of bedtime)th\"}]"
]

Which is an array of size one, with one string as its content.

You are expecting the array inside the string that has been string-encoded, which should look like this:

[
  {
    "timeFrom": "06: 00: 00",
    "timeTo": "09: 00: 00",
    "title": "First"
  },
  {
    "timeFrom": "09: 00: 00",
    "timeTo": "12: 00: 00",
    "title": "Second"
  },
  {
    "timeFrom": "12: 00: 00",
    "timeTo": "16: 00: 00",
    "title": "Third"
  },
  {
    "timeFrom": "16: 00: 00",
    "timeTo": "20: 00: 00",
    "title": "Fourth"
  },
  {
    "timeFrom": "20: 00: 00",
    "timeTo": "21: 30: 00",
    "title": "5th"
  },
  {
    "timeFrom": "21: 30: 00",
    "timeTo": "00: 00: 00",
    "title": "Dessert(withintwohoursofbedtime)th"
  }
]

Inspect the Javascript code, it seems like there is a section that is wrapping the array you want inside a string and putting that in another array. Inspect for an unnecessary call to JSON.stringify()

Sign up to request clarification or add additional context in comments.

Comments

0

You have to parse the json object to a Java object, then you can read the data.

See the JSON DOC for a list of functions you can use in Java.

3 Comments

I have already tried something like this: for (int i = 0; i < MyService.Meals.length(); ++i) { JSONObject rec = MyService.Meals.getJSONObject(i); String text = rec.getString("timeFrom"); } but don't work and I am not sure why
And what was the result @DusanMalic ?
I tried to log it but it would't log, and than I saw that MyService.Meals.length() (that is json object) is 1, but I have no idea why...

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.