需要将json键值对转换为标准数组 [英] Need to convert json key-value pairs to standard array

查看:77
本文介绍了需要将json键值对转换为标准数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 jquery 的 parseJSON() 函数将一个 json 字符串插入到一个变量中.问题是,它将我的数据转换为对象而不是二维数组.例如,

I'm interperting a json string into a variable using jquery's parseJSON() function. The problem is, it's turning my data into an object instead of a 2d array. Eg,

myData = $.parse(JSON(data));
myData.name// = "Bob"

问题是,名称"不应该是(假设这是正确的术语).相反,它应该是:

The problem is, "name" is not supposed to be a key (assuming that is the correct term). Instead, it should be:

myData[0] // = "name"
myData[1] // = "Bob"

我将如何转换它?或者是否有与使用 for 循环遍历数组索引不同的方法(但仍然能够像在二维数组中一样访问键和值作为字符串).

How would I convert this? Or is there a different method than using a for loop to walk through the index of an array (but still be able to access both key and value as a string, as you would in a 2d array).

这是一些正在使用的 json(注意它更长).这就是给数据"的东西

This is some json that is in use (Note it's MUCH longer). This is what is given for "data"

{"feat_3":"4356","feat_4":"45","feat_5":"564","feat_6":"7566"}

推荐答案

一旦你反序列化了数据(例如,你有 myData,它是一个对象),你可以遍历它的键使用 for..in,然后构建一个数组结合键和值:

Once you've deserialized the data (e.g., you have myData, which is an object), you can loop through its keys using for..in, and then build up an array that combines keys and values:

var myData, dataArray, key;
myData = $.parse(JSON(data));
dataArray = [];
for (key in myData) {
    dataArray.push(key);         // Push the key on the array
    dataArray.push(myData[key]); // Push the key's value on the array
}

由于 myData 是对 data 中的 JSON 进行反序列化的结果,我们知道 myData 是一个通用对象(例如,只是一个 {} 而不是 new Foo 或类似的东西),所以我们甚至不需要 hasOwnProperty.如果我们不知道,并且只想枚举 myData自己的 键和值,我们将添加一个 hasOwnProperty 检查:

Since myData is the result of deserializing the JSON in data, we know that myData is a generic object (e.g., just a {} as opposed to a new Foo or something like that), so we don't even need hasOwnProperty. If we didn't know that, and we only wanted to enumerate myData's own keys and values, we would add a hasOwnProperty check:

var myData, dataArray, key;
myData = $.parse(JSON(data));
dataArray = [];
for (key in myData) {
    if (myData.hasOwnProperty(key)) {
        dataArray.push(key);         // Push the key on the array
        dataArray.push(myData[key]); // Push the key's value on the array
    }
}

在你的情况下没有理由这样做,除非有人一直在玩弄Object.prototype(在这种情况下,把他们带到木棚后面,给他们一个严重的隐藏,然后他们写道:我不会在黑板上用 Object.prototype 胡说八道),但是每当你使用 for..in 时,停下来思考总是好的A) 对象是否保证是 vanilla 的,B) 如果不是,您是只想要它自己的属性,还是还想要它继承的属性?

There's no reason to do that in your case, unless someone has been mucking about with Object.prototype (in which case, take them behind the woodshed, give them a severe hiding, and then have them write "I will not muck about with Object.prototype several hundred times on the chalkboard), but whenever you use for..in, it's always good to stop and think whether A) The object is guaranteed to be vanilla, and B) If not, do you want only its own properties, or do you also want ones it inherits?

这篇关于需要将json键值对转换为标准数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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