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

Dusan Malic
  • 231
  • 4
  • 15

2 Answers2

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

Klazen108
  • 690
  • 4
  • 19
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.

qwertynl
  • 3,912
  • 1
  • 21
  • 43
  • 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 – Dusan Malic Nov 14 '13 at 21:16
  • And what was the result @DusanMalic ? – qwertynl Nov 14 '13 at 21:16
  • 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... – Dusan Malic Nov 14 '13 at 21:17