通过添加键修改Json结果

时间:2015-07-27 14:22:58

标签: javascript jquery json

我如何从

更改我的json结果
["Published with github","ZA Publications","Apress","Really","Neal"]

[
    {"Name" : "Published with github"},
    {"Name" : "ZA Publications"},
    {"Name" : "Apress"},
    {"Name" : "Really"},
    {"Name" : "Neal"}
]

如果我在某处错了,请纠正我,如果你需要更多细节

谢谢

2 个答案:

答案 0 :(得分:0)

您可以使用jQuery' map函数将其转换为对象数组

var x = ["Published with github","ZA Publications","Apress","Really","Neal"];

var y = $(x).map(function(i,v){
    return {'name':v};
});

FIDDLE

答案 1 :(得分:0)

您可以将此功能用作起点。了解它后,您可以使用MAP。

function addName() {
 var json1 = ["Published with github","ZA Publications","Apress","Really","Neal"];
 for(var i = 0; i < json1.length; i++)
 {
  // Change "Published with github" to "Name: Published with github"
  json1[i] = { Name: json1[i] };
 };
}