0

ive got this kind of json sent by a

{
   "media":{
      "1369725029":{
         "id_profil":"5738",
         "photo_profil":"http:\/\/www.mupiz.com\/5738\/picture",
         "phrase_feed":"a ajout\u00e9 une nouvelle chanson",
         "nom_media":"La Douleur (chanson sur mon tableau)",
         "nom_profil":"KRISS",
         "url_profil":"kriss",
         "streaming":"\r\n  <div class=\"playerMedia\" onclick=\"InlinePlayer(this)\" data-url=\"http:\/\/www.mupiz.com\/mp3\/5738\/mp3_51550.mp3\" data-id=\"mp3_51550.mp3\">La Douleur (chanson sur mon tableau)<\/div>\r\n",
         "url_media":"http:\/\/www.mupiz.com\/kriss\/la-douleur-chanson-sur-mon-tab"
      },
      "1369723360":{
         "id_profil":"5738",
         "photo_profil":"http:\/\/www.mupiz.com\/5738\/picture",
         "phrase_feed":"a ajout\u00e9 une nouvelle chanson",
         "nom_media":"On the Hi-Way (chanson)",
         "nom_profil":"KRISS",
         "url_profil":"kriss",
         "streaming":"\r\n  <div class=\"playerMedia\" onclick=\"InlinePlayer(this)\" data-url=\"http:\/\/www.mupiz.com\/mp3\/5738\/mp3_54344.mp3\" data-id=\"mp3_54344.mp3\">On the Hi-Way (chanson)<\/div>\r\n",
         "url_media":"http:\/\/www.mupiz.com\/kriss\/on-the-hi-way-chanson1"
      },
      "1368389617":{
         "id_profil":"32236",
         "photo_profil":"http:\/\/www.mupiz.com\/32236\/picture",
         "phrase_feed":"a ajout\u00e9 une nouvelle vid\u00e9o",
         "nom_media":"P!nk - Just Give Me A Reason ft Nate Ruess (Real Chanty Cover) ",
         "nom_profil":"Real Chanty",
         "url_profil":"RealChanty",
         "streaming":"<iframe width=\"270px\" height=\"200px\" src=\"http:\/\/www.youtube.com\/embed\/xiDzmc59fjg\" frameborder=\"0\" allowfullscreen><\/iframe>",
         "url_media":"http:\/\/www.mupiz.com\/RealChanty\/video\/p-nk-just-give-me-a-reason-ft-nate-ruess-real-chanty-cover"
      },
      "1368384065":{
         "id_profil":"1388",
         "photo_profil":"http:\/\/www.mupiz.com\/1388\/picture",
         "phrase_feed":"a ajout\u00e9 une nouvelle chanson",
         "nom_media":"On the Hi-Way (chanson)",
         "nom_profil":"Fred.Baz1-Compositeur-Bassiste-Guitariste",
         "url_profil":"fred-bazin",
         "streaming":"\r\n  <div class=\"playerMedia\" onclick=\"InlinePlayer(this)\" data-url=\"http:\/\/www.mupiz.com\/mp3\/1388\/mp3_49786.mp3\" data-id=\"mp3_49786.mp3\">On the Hi-Way (chanson)<\/div>\r\n",
         "url_media":"http:\/\/www.mupiz.com\/fred-bazin\/on-the-hi-way-chanson"
      }
   }
}

Chrome get the "right" order FF does not,

Any ideas ?

Mike Christensen
  • 88,082
  • 50
  • 208
  • 326
SoCkEt7
  • 2,237
  • 5
  • 22
  • 30

2 Answers2

5

I don't think you can rely on the ordering of properties within a JSON object - JSON can come back in any order.

From the JSON specification at http://www.json.org/

An object is an unordered set of name/value pairs

Possible duplicate? JSON order mixed up

Community
  • 1
  • 1
calipoop
  • 792
  • 9
  • 31
  • How can i with $.each keep this order ? – SoCkEt7 May 30 '13 at 02:13
  • 1
    @user1965147 you cant do that with this current setup. This cant be converted as an array as well. I guess you'll have to just access it by their names like this : `json.media["1369725029"]` (if you want to choose the 1st object in your json) – krishwader May 30 '13 at 04:52
1

No, its not necessary for the properties to be on order simply because JS doesn't maintain the order. The general thought is since these are key value pairs, order needn't be maintained. An object array, on the other hand can and will maintain the order because its bound by an index and is iteratable. This is how an object array looks like :

[
     { "name" : "test", "age" : 10 }, //index 0
     { "name" : "test1" ,"age": 15 } //index 1

 ]
krishwader
  • 11,341
  • 1
  • 34
  • 51