需要JSON键值对转换为标准阵列 [英] Need to convert json key-value pairs to standard array
问题描述
我interperting一个JSON字符串使用jQuery的parseJSON()函数的变量。问题是,它把我的数据转换成一个对象,而不是一个二维数组。例如,
的myData = $ .parse(JSON(数据));
myData.name// =鲍勃
问题是,名不应该是一个键(假设是正确的术语)。相反,它应该是:
myData的[0] // =名
MYDATA的[1] // =鲍勃
我怎么会转换呢?还是有不同的方法比使用for循环通过数组的索引行走(但仍能够访问这两个键和值作为一个字符串,就像在一个二维数组)。
编辑:这是一些JSON即正在使用中(注意这是更长)。这就是对数据
给出 {feat_3:4356,feat_4:45,feat_5:564,feat_6:7566}
一旦你反序列化数据(例如,你有 myData的
,这是一个对象)你可以通过使用键循环 的for..in
,然后建立,结合键和值的数组:
VAR myData的,dataArray中,关键的;
myData的= $ .parse(JSON(数据));
dataArray中= [];
对(在MYDATA的键){
dataArray.push(键); //按下该键的阵列上
dataArray.push(MYDATA的[关键]); //推阵列上的键值
}
由于 myData的
是数据反序列化JSON的结果
,我们知道, myData的
是一个通用对象(例如,仅仅是 {}
而不是一个新富
或类似的东西),所以我们甚至不需要的hasOwnProperty
。如果我们不知道,我们只希望列举 myData的
的的键和值,我们将增加一个的 的hasOwnProperty
检查:
VAR myData的,dataArray中,关键的;
myData的= $ .parse(JSON(数据));
dataArray中= [];
对(在MYDATA的键){
如果(myData.hasOwnProperty(键)){
dataArray.push(键); //按下该键的阵列上
dataArray.push(MYDATA的[关键]); //推阵列上的键值
}
}
有没有理由这样做,在你的情况,除非有人一直摆弄的Object.prototype
(在这种情况下,把他们木棚后面,给他们一个严重的隐藏,然后让他们写我不会和的Object.prototype
在黑板上几百倍),但每当你使用的渣土约..in
,它总是好停下来思考的对象是否A)保证是香草,和B)如果没有,你只想要自己的属性,或者你也想那些它继承?
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"
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).
EDIT: 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"}
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
}
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
}
}
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屋!