[英]How to convert from a JSON array to a JSON object
I have this JSON that I'm getting back from a web service call: 我有这个JSON,我从Web服务调用回来:
{
"name": "My Name",
"path": "my path",
"id": "44",
"type": "my type",
"classes": "my classes"
},
{
"name": "his Name",
"path": "his path",
"id": "76",
"type": "his type",
"classes": "his classes"
}
I then need to convert it to this format 然后我需要将其转换为这种格式
{
"44" : { "name" : "My Name", "path" : "my path" },
"76" : { "name" : "his Name", "path" : "his path" }
}
My initial naive attempt was this: 我最初的天真尝试是这样的:
var myData = [];
for (var i = 0; i < rawData.length; i++) {
myData.push({
rawData[i].id :
{
"path": rawData[i].path,
"name": rawData[i].name
}
});
which fails with syntax errors, so I eventually got to this: 因语法错误而失败,所以我最终得到了这个:
var myData = [];
for (var i = 0; i < rawData.length; i++) {
myData.push(rawData[i].id,
{
"path": rawData[i].path,
"name": rawData[i].name
});
and it mostly works. 它主要起作用。 My array is populated, but the problem is that my
myData
array doesn't have the "44", and "76" part of the object, just the { "name" : "", "path" : "" }
part. 我的数组已填充,但问题是我的
myData
数组没有对象的“44”和“76”部分,只有{ "name" : "", "path" : "" }
部分。 I expect this is due to a lack of understanding on my part of how JSON and javscript objects work. 我希望这是因为我对JSON和javscript对象的工作方式缺乏了解。
Don't use Array.prototype.push()
, use the square bracket notation and define your output as an object not an array. 不要使用
Array.prototype.push()
,使用方括号表示法并将输出定义为对象而不是数组。
var myData = {};
for (var i = 0; i < rawData.length; i++) {
myData[rawData[i].id] = {
"path": rawData[i].path,
"name": rawData[i].name
}
}
Your desired output isn't an array, so that's your starting point. 您想要的输出不是数组,所以这是您的起点。 The output you've said you want is an object , not an array.
你说你想要的输出是一个对象 ,而不是一个数组。
You build your result by creating a blank object and then adding the objects to it using id
as the key: 您可以通过创建空白对象然后使用
id
作为键将对象添加到其中来构建结果:
var myData = {};
rawData.forEach(function(entry) {
myData[entry.id] = {
name: entry.name,
path: entry.path
};
});
Or if you don't want to use forEach
(it's ES5, but can be shimmed for older browsers), the old-fashioned way: 或者,如果您不想使用
forEach
(它是ES5,但可以为旧浏览器填充),那么过时的方式:
var myData = {};
var index, entry;
for (index = 0; index < rawData.length; ++index) {
entry = rawData[index];
myData[entry.id] = {
name: entry.name,
path: entry.path
};
}
You need to convert your id to a string? 你需要将你的id转换为字符串?
var myData = {};
for (var i = 0; i < rawData.length; i++) {
myData[String(rawData[i].id)] = {
"path": rawData[i].path,
"name": rawData[i].name
};
}
A variation on what other posters have written: 其他海报写的变化:
// Create a new empty object.
var out = {};
// Loop over your array of objects
// Add the each object id as a key to the output object, and the object as the value.
for (var i = 0, l = arr.length; i <l; i++) {
var obj = arr[i];
out[obj.id] = obj;
// Delete the properties from the newly added object you don't want.
delete obj.id;
delete obj.type;
delete obj.classes;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.