排序JSON OBJECT(Firefox中的不同顺序)

时间:2013-12-30 13:52:28

标签: javascript ajax json

我想用键对json对象进行排序,Chrome保留我需要的顺序,但是firefox不要。

这是我的json对象的一个​​例子:

{
  "media": {
    "1387185503": {
      "id_profil": "7256",
      "photo_profil": "http://www.mupiz.com/7256/picture",
      "phrase_feed": "a ajouté une nouvelle vidéo",
      "nom_media": "Jam a la casserole",
      "nom_profil": "Camille Boullier de Branche",
      "url_profil": "camille-boullier-de-branche",
      "streaming": "<iframe width=\"270\" height=\"200\" src=\"http://www.youtube.com/embed/K6i_avCwsvQ?html5=1\" frameborder=\"0\" allowfullscreen></iframe>",
      "url_media": "http://www.mupiz.com/camille-boullier-de-branche/video/jam-a-la-casserole"
    },
    "1387185460": {
      "id_profil": "7256",
      "photo_profil": "http://www.mupiz.com/7256/picture",
      "phrase_feed": "a ajouté une nouvelle vidéo",
      "nom_media": "festival silhouette 2010 avec IGIT",
      "nom_profil": "Camille Boullier de Branche",
      "url_profil": "camille-boullier-de-branche",
      "streaming": "<object width=\"270px\" height=\"200px\" ><param name=\"movie\" value=\"http://www.dailymotion.com/swf/xet02y&related=0&colors=background:000000;foreground:FFFFFF;special:88868A;\"></param><param name=\"wmode\" value=\"transparent\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowScriptAccess\" value=\"always\"></param><embed src=\"http://www.dailymotion.com/swf/xet02y&related=0&colors=background:000000;foreground:FFFFFF;special:88868A;\" type=\"application/x-shockwave-flash\" width=\"270\" wmode=\"transparent\"  height=\"200\" allowfullscreen=\"true\" allowscriptaccess=\"always\"></embed></object>",
      "url_media": "http://www.mupiz.com/camille-boullier-de-branche/video/festival-silhouette-2010-avec-igit"
    },
    "1385912810": {
      "id_profil": "36541",
      "photo_profil": "http://www.mupiz.com/36541/picture",
      "phrase_feed": "a ajouté une nouvelle chanson",
      "nom_media": "Sans titre",
      "nom_profil": "Eve IBK",
      "url_profil": "evelyne-zyss",
      "streaming": "\r\n  <div class=\"playerMedia\" onclick=\"InlinePlayer(this)\" data-url=\"http://www.mupiz.com/mp3/36541/mp3_85725.mp3\" data-id=\"mp3_85725.mp3\">Sans titre</div>\r\n",
      "url_media": "http://www.mupiz.com/evelyne-zyss/"
    }, .....

我想用时间戳asc而不是desc来对我的json对象的这一部分进行排序。

有什么想法吗?

由于

1 个答案:

答案 0 :(得分:2)

浏览器在“strigify”数字时自动排序“JSON属性:

例如,此功能在 Chrome Firexfox 中具有相同的行为:

 var an_obj = { "4": "d", "1": "a", "2": "c"};
 alert(Object.keys(an_obj)); //will alert 1, 2, 4

如果是您的Sample JSON

Chrome:

var an_obj = { "1387185460": "a", "1387185503": "b", "1385912810": "c"};
alert(Object.keys(an_obj)); // will alert "1385912810, 1387185460, 1387185503"

Firefox 时,它实际上保留了原始状态:

var an_obj = { "1387185460": "a", "1387185503": "b", "1385912810": "c"};
 alert(Object.keys(an_obj)); // will alert "1387185460, 1387185503, 1385912810"

这是因为Firefox排序算法仅在前3个字符不完全相同时才起作用。例如:

Chrome

  var value2 = {"1003":1, "1001":2, "1000":3};
  alert(Object.keys(value2)); // will alert 1000, 1001, 1003

Firefox 将保留原始订单:

 var value2 = {"1003":1, "1001":2, "1000":3};
 alert(Object.keys(value2)); // will alert 1003, 1001, 1000

这实际上是Firefox团队的known issue。枚举排序未指定为ES6的一部分,因此您不应该依赖它。预计这将在ES7实施中得到解决。

关于你手头的问题,我会这样做:

var arr = {
        "media": {
            "1387185503": {
                "id_profil": "7256",
                "photo_profil": "http://www.mupiz.com/7256/picture",
                "phrase_feed": "a ajouté une nouvelle vidéo",
                "nom_media": "Jam a la casserole",
                "nom_profil": "Camille Boullier de Branche",
                "url_profil": "camille-boullier-de-branche",
                "streaming": "<iframe width=\"270\" height=\"200\" src=\"http://www.youtube.com/embed/K6i_avCwsvQ?html5=1\" frameborder=\"0\" allowfullscreen></iframe>",
                "url_media": "http://www.mupiz.com/camille-boullier-de-branche/video/jam-a-la-casserole"
            },
            "1387185460": {
                "id_profil": "7256",
                "photo_profil": "http://www.mupiz.com/7256/picture",
                "phrase_feed": "a ajouté une nouvelle vidéo",
                "nom_media": "festival silhouette 2010 avec IGIT",
                "nom_profil": "Camille Boullier de Branche",
                "url_profil": "camille-boullier-de-branche",
                "streaming": "<object width=\"270px\" height=\"200px\" ><param name=\"movie\" value=\"http://www.dailymotion.com/swf/xet02y&related=0&colors=background:000000;foreground:FFFFFF;special:88868A;\"></param><param name=\"wmode\" value=\"transparent\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowScriptAccess\" value=\"always\"></param><embed src=\"http://www.dailymotion.com/swf/xet02y&related=0&colors=background:000000;foreground:FFFFFF;special:88868A;\" type=\"application/x-shockwave-flash\" width=\"270\" wmode=\"transparent\"  height=\"200\" allowfullscreen=\"true\" allowscriptaccess=\"always\"></embed></object>",
                "url_media": "http://www.mupiz.com/camille-boullier-de-branche/video/festival-silhouette-2010-avec-igit"
            },
            "1385912810": {
                "id_profil": "36541",
                "photo_profil": "http://www.mupiz.com/36541/picture",
                "phrase_feed": "a ajouté une nouvelle chanson",
                "nom_media": "Sans titre",
                "nom_profil": "Eve IBK",
                "url_profil": "evelyne-zyss",
                "streaming": "\r\n  <div class=\"playerMedia\" onclick=\"InlinePlayer(this)\" data-url=\"http://www.mupiz.com/mp3/36541/mp3_85725.mp3\" data-id=\"mp3_85725.mp3\">Sans titre</div>\r\n",
                "url_media": "http://www.mupiz.com/evelyne-zyss/"
            }
        }
    };
 var properties=Object.keys( arr.media).sort();
 arr.media = properties.reduce(function(res, prop){
           res[prop] = arr.media[prop];
           return res;
       },{});

和替代解决方案是实现toJSON:

arr.media.toJSON = function(){
            var newMedia = properties.reverse().reduce(function(res, prop){
                res[prop] = arr.media[prop];
                return res;
            },{});
           return newMedia;
       };
 var newSortedObject =(JSON.stringify(arr.media));

希望这有帮助