简体   繁体   English

需要将json键值对转换为标准数组

[英]Need to convert json key-value pairs to standard array

I'm interperting a json string into a variable using jquery's parseJSON() function. 我正在使用jquery的parseJSON()函数将json字符串插入到变量中。 The problem is, it's turning my data into an object instead of a 2d array. 问题是,它将我的数据转换为对象而不是2d数组。 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). 或者是否有一种不同的方法,而不是使用for循环遍历数组的索引(但仍然能够像字符串一样访问键和值,就像在二维数组中一样)。

EDIT: This is some json that is in use (Note it's MUCH longer). 编辑:这是一些正在使用的json(注意它的时间更长)。 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 (eg, 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: 一旦你反序列化数据(例如,你有myData ,这是一个对象),你可以使用for..in循环其键,然后构建一个组合键和值的数组:

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 (eg, just a {} as opposed to a new Foo or something like that), so we don't even need hasOwnProperty . 由于myData是反序列化data JSON的结果,我们知道myData是一个通用对象(例如,只是一个{}而不是new Foo或类似的东西),所以我们甚至不需要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: 如果我们不知道这一点,并且我们只想枚举myData 自己的键和值,我们会添加一个hasOwnProperty检查:

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? 在你的情况下没有理由这样做,除非有人一直在使用Object.prototype (在这种情况下,把它们带到木棚后面,给他们一个严重的隐藏,然后让他们写“我不会捣乱与Object.prototype在黑板上几百次),但每当你使用for..in ,停止并思考A)对象是否保证是香草,并且B)如果不是,你想要只有它自己的属性总是好的,或者你也想要它继承的?

var data = $.parse(JSON({"feat_3":"4356","feat_4":"45","feat_5":"564","feat_6":"7566"}));

var arr = [];

for( var i in data ) { 
  if( data.hasOwnProperty( i ) ){ 
    arr.push( i,  data[i] );
  }
}

Array will be : 数组将是:

["feat_3", "4356", "feat_4", "45", "feat_5", "564", "feat_6", "7566"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM