在json解析中停止更改Order

时间:2016-12-17 21:39:18

标签: javascript json parsing

这是基本的字符串响应:

response = "[{"prefixtodomid":"Sat17Dec2016103310GMT","todo_title":"task 3 changed","is_done_todo":false,"todo_subtitle_field":"\u00a0","prefix_pro_due_date":"","multicheckbox":false,"project_file_list":""},{"prefixtodomid":"Sat17Dec2016103313GMT","todo_title":"ce","is_done_todo":false,"todo_subtitle_field":"\u00a0","prefix_pro_due_date":"","multicheckbox":false,"project_file_list":""},{"prefixtodomid":"Sat17Dec2016103318GMT","todo_title":"dewdw","is_done_todo":false,"todo_subtitle_field":"\u00a0","prefix_pro_due_date":"","multicheckbox":false,"project_file_list":""},{"prefixtodomid":"Sat17Dec2016103321GMT","todo_title":"task 4","is_done_todo":false,"todo_subtitle_field":"\u00a0","prefix_pro_due_date":"","multicheckbox":false,"project_file_list":""},{"prefixtodomid":"Sat17Dec2016181953GMT","todo_title":"task 5","is_done_todo":false,"todo_subtitle_field":"\u00a0","prefix_pro_due_date":"","multicheckbox":false,"project_file_list":{"43":"http:\/\/example\/intra\/wp-content\/uploads\/2016\/11\/1project.png","26":"http:\/\/example\/intra\/wp-content\/uploads\/2016\/11\/2016--\u2039-Le-Blog-OSD-\u2014-WordPress_437.png"}},{"prefixtodomid":"Sat17Dec2016181957GMT","todo_title":"cewcwcwecw","todo_subtitle_field":"\u00a0","project_file_list":{"26":"http:\/\/example\/intra\/wp-content\/uploads\/2016\/11\/2016-10-16-15_26_04-Unyson-\u2039-Le-Blog-OSD-\u2014-WordPress_437.png"}}]"

我将它解析为json,如:

var obj = jQuery.parseJSON( response );

现在我正在循环:

for (var i = 0; i<Object.keys(obj).length; i++) { 
                (function(index){

                    var haveimage = obj[i].project_file_list;

                    if(haveimage){
                        // other logic
                    }

                })(i); // pass the value of i
            }

这里hasimage总是按照图片的id顺序显示图片,即使我们在响应字符串中更改顺序,parseJSON方法只是再次按id更改顺序,有没有解决方法可以解决这个问题?

如果没有,我还能用什么来代替parseJSON?

谢谢:)

1 个答案:

答案 0 :(得分:3)

对象是一组无序的名称/值对。

您可以使用数组来维护订单:

[
   {
      "43":"http:\/\/example\/intra\/wp-content\/uploads\/2016\/11\/1project.png"
   },
   {
      "26":"http:\/\/example\/intra\/wp-content\/uploads\/2016\/11\/2016--\u2039-Le-Blog-OSD-\u2014-WordPress_437.png"
   }
]