[英]How to convert JSON Object into Javascript array
Linked to this question and this one also , I would like to know how to do the exact same thing but with a different type of JSON : 联系到这个问题,并且这其中也 ,我想知道如何做同样的事情,但有一个不同类型的JSON的:
Here my JSON Object : 这是我的JSON对象:
var myDb = {
"04c85ccab52880": {
"name": "name1",
"firstname": "fname1",
"societe": "soc1"
},
"04c95ccab52880": {
"name": "name2",
"firstname": "fname2",
"societe": "soc2"
},
"048574220e2a80": {
"name": "name3",
"firstname": "fname3",
"societe": "soc3"
}
};
My problem is that my JSON is different from their JSON, actually I have an array of array in mine. 我的问题是我的JSON与他们的JSON不同,实际上我的数组中有一个数组。 So how to convert this into a Javascript array ?
那么如何将其转换为Javascript数组呢?
var arr = [];
$.each( myDb, function( key, value ) {
arr.push( value );
});
console.log(user_list);
This kind of script seems to return my 3 different objects but where are my uid from JSON ? 这种脚本似乎返回了3个不同的对象,但是JSON中的uid在哪里? How can I get them ?
我怎样才能得到它们? Because now my keys are 0,1,2 and no longer my uid.
因为现在我的密钥是0,1,2,而不再是我的uid。
Any ideas ? 有任何想法吗 ? Thanks
谢谢
A working JSFIDDLE 工作中的JSFIDDLE
JS code: JS代码:
//var arr = [];
var myDb = {
"04c85ccab52880": {
"name": "name1",
"firstname": "fname1",
"societe": "soc1"
},
"04c95ccab52880": {
"name": "name2",
"firstname": "fname2",
"societe": "soc2"
},
"048574220e2a80": {
"name": "name3",
"firstname": "fname3",
"societe": "soc3"
}
};
$.each( myDb, function( key, value ) {
//arr.push( value );
console.log("key => "+key);//will output: 04c85ccab52880 and all such
$.each( value, function( ky, val ) {
console.log('ky => '+ky);//will output: name, firstname, societe
console.log('val => '+val);//will output: name1, fname1, soc1
});
});
if you want to convert them, with key. 如果要转换它们,请使用键。 I suggest something like below
我建议如下
var mapped = Object.keys( myDb ).map(function( uid ){
return (myDb[uid ].uid = uid ) && myDb[uid ];
});
for value in your array look like, example mapped[0]
has value : 对于数组中的值,示例
mapped[0]
具有value:
{
"name": "name1",
"firstname": "fname1",
"societe": "soc1",
"uid": "04c85ccab52880"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.