javascript 将 JSON 转换为关联数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20396006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert JSON to Associative array
提问by JunM
I want to convert a JSON response to associative array but looks like I was not able to make it right.
我想将 JSON 响应转换为关联数组,但看起来我没能做到。
Here is my JSON sample response:
这是我的 JSON 示例响应:
{
"response":[
{
"id":100,
"certificate":{
"id":1,
"title":"Lorem ipsum dolor sit amet, consectetur adipiscing elit."
},
"created_at":"2013-12-02T15:20:08.233Z"
},
{
"id":101,
"certificate":{
"id":2,
"title":"Aenean facilisis, nisl vitae pulvinar varius."
},
"created_at":"2013-12-02T15:20:08.240Z"
}
],
"count":2
}
This is what I have tried so far:
这是我迄今为止尝试过的:
var len = obj.response.length;
var rData = [];
var gcData = [];
for(var i = 0; i < len; i++){
rData[i] = $.map(obj.response[i], function(value, index) {
if(typeof value=="object"){
gcData = $.map(value, function(value1, index) {
return [value1];
});
return gcData;
}else{
return [value];
}
});
}
My expected output:
我的预期输出:
rData = [
100 : [
id: ["100"],
certificate: [
id: ["1"],
title: ["Lorem ipsum dolor sit amet, consectetur adipiscing elit."]
],
created_at: ["2013-12-02T15:20:08.240Z"]
]
101 : [
id: ["101"],
certificate: [
id: ["2"],
title: ["Aenean facilisis, nisl vitae pulvinar varius."]
],
created_at: ["2013-12-02T15:20:08.240Z"]
]
]
Please help. Thanks.
请帮忙。谢谢。
回答by Felix Kling
I just want to group it by keys something like I have in expected output
我只想按预期输出中的键对其进行分组
It seems you want to create an id -> object
map. To do that you just have to iterate over the array, take the id
attribute of each object as property name and assign the object (the element of the array) to that property of the map.
看来您要创建id -> object
地图。为此,您只需遍历数组,将id
每个对象的属性作为属性名称,并将对象(数组的元素)分配给映射的该属性。
Example:
例子:
var map = {};
var response = obj.response;
for (var i = 0, l = response.length; i < l; i++) {
map[response[i].id] = response[i];
}
console.log(map);
Each object inside the array is already in the structure you want it to be. The output is
数组中的每个对象都已经在你想要的结构中。输出是
{
"100": {
"id": 100,
"certificate": {
"id": 1,
"title": "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
},
"created_at": "2013-12-02T15:20:08.233Z"
},
"101": {
"id": 101,
"certificate": {
"id": 2,
"title": "Aenean facilisis, nisl vitae pulvinar varius."
},
"created_at": "2013-12-02T15:20:08.240Z"
}
}
回答by PA.
your desired JSON is incorrect.
您想要的 JSON 不正确。
It isn't clear if you want to create an array of arrays or an object of arrays or what. To help decide which option fits your need, try this code
目前尚不清楚您是要创建数组数组还是数组对象或什么。要帮助确定哪个选项适合您的需要,请尝试此代码
var a=[];
var o={};
for (var i=0, l=response.length; i<l; i++) {
var e = response[i];
a[e.id] = e.certificate;
o[e.id] = e.certificate;
}
console.log(JSON.stringify(a));
console.log(JSON.stringify(o));