0

First have a look here, with Google Chrome and then FireFox: http://jsbin.com/aherar/1/edit Watch the order of the numbers.

JSON example:

{
     "3259341": {
         "datum": "2013-07-23 14:55:00",
         "titel": "Open Space"
     },
     "3259609": {
         "datum": "2013-07-23 14:50:00",
         "titel": "Something else"
     },
     "3257403": {
         "datum": "2013-07-23 14:45:00",
         "titel": "High-Tech"
     }
 }

If I try to iterate over the JSON-Object with Chrome or Opera Next (WebKit) the "for in" runs reverse over the JSON properties. FireFox or Safari (Desktop or iOS) runs correctly, right order.

Is there a way to do it like expected, like FireFox do?

Fabio Poloni
  • 8,219
  • 5
  • 44
  • 74
Robert
  • 664
  • 9
  • 25

1 Answers1

0

JSON objects are unordered sets of name/value pairs, you can't (and shouldn't!) rely on any certain order of elements.

A.O.
  • 3,733
  • 6
  • 30
  • 49
  • It is interesting, cause Android 2.3.3 Browser iterates correct, Android 4.2.2 does not. So the same with Safari instead of Chrome. – Robert Jul 23 '13 at 17:56
  • the point is that there is no "correct"....if you want a consistent order you should take Rob W's advice and add a field that stores the order you prefer... – A.O. Jul 23 '13 at 17:58
  • Now I found this: http://stackoverflow.com/questions/7214293/is-the-order-of-elements-in-a-json-list-maintained. So I have to order it with an array. – Robert Jul 23 '13 at 18:00
  • yes, then you will have an array of json objects. but how will you store each of those objects in the array? you should really consider just adding an "order" field, it will make life much easier :) – A.O. Jul 23 '13 at 18:05
  • You're right, I meaned I have to order it with one of the properties I want. So in this case I will choose the date. It is so time wasting to do this. I only wanted to load it via AJAX and iterate over the keys, thats all. – Robert Jul 23 '13 at 18:07