我要如何扭转JSON在JavaScript中? [英] How do i reverse JSON in JavaScript?

查看:101
本文介绍了我要如何扭转JSON在JavaScript中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  [
   {任务:测试,创造:/日期(1291676980607)/},
   {任务:有更多的大测试,创造:/日期(1291677246057)/},
   {任务:新任务,创造:/日期(1291747764564)/}
]

我看着就到这里,有人有相同的这类问题,但选中正确答案是,这将是不同的IE浏览器,如果该项目被删除,这将是罚款。我的问题是,上述这些项目都存储,但是当我去抓住他们,迭代,并返回,该项目被逆转,创建是在 0 指数任务 1 。此外,我需要返回此作为JSON。

下面是我的基本JS(价值==一个的 INT 的用户传递):

  outputJSON = {};
为(X在JSON [值]){
    outputJSON [X] = _objectRevival(JSON [值] [X]);
}
返回outputJSON;

返回:

 创建:周一至2010年12月6日十五点09分40秒格林尼治标准​​时间0800(太平洋标准时间)
任务:测试


解决方案

一个对象的属性的顺序是不确定的。这是不可能的,迫使他们在一个指定的顺序。如果你需要他们在一个特定的顺序,可以可靠地建立这种结构使用数组:

  VAR值= [
   [任务,测试],[创造,/日期(1291676980607)/]],
   [任务,一个更加大测试],[创造,/日期(1291677246057)/]],
   [任务,新建任务],[创造,/日期(1291747764564)/]]
];

然后你就可以在你的结构,这样的循环:

 为(VAR I = 0; I< values​​.length;我++){
    对于(VAR K = 0; K<值[I]; k ++){
        //值[I] [K] [0]包含标签(索引0)
        //值[I] [k]的[1]包含值(指数1)
    }
}

[
   {"task":"test","created":"/Date(1291676980607)/"},
   {"task":"One More Big Test","created":"/Date(1291677246057)/"},
   {"task":"New Task","created":"/Date(1291747764564)/"}
]

I looked on here, and someone had the same sort of question, but the "checked" correct answer was that it will be different on IE if the item is deleted, which would be fine. My issue is, those items above are stored, but when i go and grab them, iterate, and return, the items are reversed and the created is at the 0 index and task is at 1. Also, i need to return this as JSON.

Here is my basic JS (value == an int the user is passing in):

outputJSON = {};
for(x in json[value]){
    outputJSON[x] = _objectRevival(json[value][x]);
}
return outputJSON;

That returns:

created: Mon Dec 06 2010 15:09:40 GMT-0800 (Pacific Standard Time)
task: "test"

解决方案

The order of the properties of an object is undefined. It is not possible to force them in a specified order. If you need them in a specific order, you can build this structure reliably using arrays:

var values = [
   [["task", "test"],              ["created", "/Date(1291676980607)/"]],
   [["task", "One More Big Test"], ["created", "/Date(1291677246057)/"]],
   [["task", "New Task"],          ["created", "/Date(1291747764564)/"]]
];

Then you can iterate over your structure like this:

for (var i = 0; i < values.length; i++) {
    for (var k = 0; k < values[i]; k++) {
        // values[i][k][0] contains the label (index 0)
        // values[i][k][1] contains the value (index 1)
    }
}

这篇关于我要如何扭转JSON在JavaScript中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆