我有这种格式的JSON
[
{"name":"universityID","value":"454"},
{"name":"grade","value":"88"},
{"name":"date","value":"15-Jan-2016"},
{"name":"address","value":"Washington"}
]
我想要我的JSON
{
"universityID":"454",
"grade":"88",
"date":"15-Jan-2016",
"address":"Washington"
}
答案 0 :(得分:0)
您只需迭代您的数组并将每个属性填充到新对象中:
var input = [
{"name":"universityID","value":"454"},
{"name":"grade","value":"88"},
{"name":"date","value":"15-Jan-2016"},
{"name":"address","value":"Washington"}
];
var output = {};
input.forEach(function(attr) {
output[attr.name] = attr.value;
});
console.log(output);
编辑:如果universityID
需要是一个数组:
var input = [
{"name":"universityID","value":"454"},
{"name":"grade","value":"88"},
{"name":"date","value":"15-Jan-2016"},
{"name":"address","value":"Washington"}
];
var output = {};
input.forEach(function(attr) {
if (attr.name == "universityID") {
if (!(attr.name in output)) output[attr.name] = [];
output[attr.name].push(attr.value);
} else {
output[attr.name] = attr.value;
}
});
console.log(output);