我有一个JSON对象,其中包含以下内容:
[
{
"_id":"5078c3a803ff4197dc81fbfb",
"email":"user1@gmail.com",
"image":"some_image_url",
"name":"Name 1"
},
{
"_id":"5078c3a803ff4197dc81fbfc",
"email":"user2@gmail.com",
"image":"some_image_url",
"name":"Name 2"
}
]
我想将“_id”键更改为“id”,以便它变为
[
{
"id":"5078c3a803ff4197dc81fbfb",
"email":"user1@gmail.com",
"image":"some_image_url",
"name":"Name 1"
},
{
"id":"5078c3a803ff4197dc81fbfc",
"email":"user2@gmail.com",
"image":"some_image_url",
"name":"Name 2"
}
]
我如何使用Javascript,jQuery或Ruby,Rails?
感谢。
答案 0 :(得分:79)
var json = '[{"_id":"5078c3a803ff4197dc81fbfb","email":"user1@gmail.com","image":"some_image_url","name":"Name 1"},{"_id":"5078c3a803ff4197dc81fbfc","email":"user2@gmail.com","image":"some_image_url","name":"Name 2"}]';
var obj = JSON.parse(json)[0];
obj.id = obj._id;
delete obj._id;
json = JSON.stringify([obj]);
这会将json更改为:
[{"email":"user1@gmail.com","image":"some_image_url","name":"Name 1","id":"5078c3a803ff4197dc81fbfb"}]
由于订单在JSON中的名称/值对中没有意义,因此这与您想要的结果相同。
答案 1 :(得分:23)
在这种情况下,最简单的方法是使用字符串替换。序列化JSON将无法正常工作,因为_id将成为对象的属性名称,更改属性名称不是一项简单的任务(至少在大多数语言中都没有,在javascript中也不是那么糟糕)。而只是做;
jsonString = jsonString.replace("\"_id\":", "\"id\":");
答案 2 :(得分:16)
正如evanmcdonnal所提到的,最简单的解决方案是将其作为字符串而不是JSON处理,
var json = [{"_id":"5078c3a803ff4197dc81fbfb","email":"user1@gmail.com","image":"some_image_url","name":"Name 1"},{"_id":"5078c3a803ff4197dc81fbfc","email":"user2@gmail.com","image":"some_image_url","name":"Name 2"}];
json = JSON.parse(JSON.stringify(json).split('"_id":').join('"id":'));
document.write(JSON.stringify(json));

这会将给定的JSON数据转换为字符串并替换" _id"到" id"然后将其转换回所需的JSON格式。但我使用split
和join
代替replace
,因为replace
只会替换字符串的第一个匹配项。
答案 3 :(得分:5)
如果要重命名某个键的所有出现次数,可以使用带g选项的正则表达式。例如:
var json = [{"_id":"1","email":"user1@gmail.com","image":"some_image_url","name":"Name 1"},{"_id":"2","email":"user2@gmail.com","image":"some_image_url","name":"Name 2"}];
str = JSON.stringify(json);
现在我们在str。
中使用字符串格式的json使用带有 g 选项的正则表达式将所有出现的“_id”替换为“id”:
str = str.replace(/\"_id\":/g, "\"id\":");
并返回json格式:
json = JSON.parse(str);
现在我们的json带有想要的密钥名称。
答案 4 :(得分:1)
如果您的对象看起来像这样:
obj = {
"_id":"5078c3a803ff4197dc81fbfb",
"email":"user1@gmail.com",
"image":"some_image_url",
"name":"Name 1"
}
JavaScript中最简单的方法可能是:
obj.id = obj._id
del object['_id']
结果,你会得到:
obj = {
"id":"5078c3a803ff4197dc81fbfb",
"email":"user1@gmail.com",
"image":"some_image_url",
"name":"Name 1"
}
答案 5 :(得分:1)
如果有人需要动态执行此操作:
jsonObject
replace
现在将具有新密钥。
重要提示:
如果您的密钥没有被if
函数更改,它将直接将其从数组中取出。您可能要在其中放置一些{{1}}语句。
答案 6 :(得分:1)
如果要替换 JSON 对象的键,请使用以下逻辑
const student= {
"key": "b9ed-9c1a04247482",
"name": "Devaraju",
"DOB" : "01/02/2000",
"score" : "A+"
}
let {key, ...new_student} = {...student}
new_student.id= key
console.log(new_student)
答案 7 :(得分:0)
如果要动态执行此操作,例如,您有一个要作为JSON对象的键应用的数组:
你的数组将是:
var strs = ['http://example.com?param', 'http://example.com?param=123', 'http://example.com?param&another','http://example.com?params'];
var rx = /[?&]param(?![^&=])/;
for (var s of strs) {
console.log(s, "=>", rx.test(s))
}
现在您有一个JSON数组,如:
var keys = ["id", "name","Address","Phone"] // The array size should be same as JSON Object keys size
您生成的JSON数组将如下:
var jArray = [
{
"_id": 1,
"_name": "Asna",
"Address": "NY",
"Phone": 123
},
{
"_id": 2,
"_name": "Euphoria",
"Address": "Monaco",
"Phone": 124
},
{
"_id": 3,
"_name": "Ahmed",
"Address": "Mumbai",
"Phone": 125
}
]
$.each(jArray ,function(pos,obj){
var counter = 0;
$.each(obj,function(key,value){
jArray [pos][keys[counter]] = value;
delete jArray [pos][key];
counter++;
})
})
答案 8 :(得分:0)
可以使用typeScript
function renameJson(json,oldkey,newkey) {
return Object.keys(json).reduce((s,item) =>
item == oldkey ? ({...s,[newkey]:json[oldkey]}) : ({...s,[item]:json[item]}),{})
}
答案 9 :(得分:0)
通过使用地图功能,您可以做到这一点。 请参考下面的代码。
var userDetails = [{
"_id":"5078c3a803ff4197dc81fbfb",
"email":"user1@gmail.com",
"image":"some_image_url",
"name":"Name 1"
},{
"_id":"5078c3a803ff4197dc81fbfc",
"email":"user2@gmail.com",
"image":"some_image_url",
"name":"Name 2"
}];
var formattedUserDetails = userDetails.map(({ _id:id, email, image, name }) => ({
id,
email,
image,
name
}));
console.log(formattedUserDetails);
答案 10 :(得分:0)
尝试一下:
let jsonArr = [
{
"_id":"5078c3a803ff4197dc81fbfb",
"email":"user1@gmail.com",
"image":"some_image_url",
"name":"Name 1"
},
{
"_id":"5078c3a803ff4197dc81fbfc",
"email":"user2@gmail.com",
"image":"some_image_url",
"name":"Name 2"
}
]
let idModified = jsonArr.map(
obj => {
return {
"id" : obj._id,
"email":obj.email,
"image":obj.image,
"name":obj.name
}
}
);
console.log(idModified);
答案 11 :(得分:0)
JSON.parse
有两个参数。第二个参数reviver是一个转换函数,可以将其格式化为所需的输出格式。请参阅ECMA规范here。
在齐磊功能中:
this
是包含正在作为此函数处理的属性,属性名称为字符串,属性值作为此函数的参数的对象。const json = '[{"_id":"5078c3a803ff4197dc81fbfb","email":"user1@gmail.com","image":"some_image_url","name":"Name 1"},{"_id":"5078c3a803ff4197dc81fbfc","email":"user2@gmail.com","image":"some_image_url","name":"Name 2"}]';
const obj = JSON.parse(json, function(k, v) {
if (k === "_id") {
this.id = v;
return; # if return undefined, orignal property will be removed
}
return v;
});
const res = JSON.stringify(obj);
console.log(res)
输出:
[{"email":"user1@gmail.com","image":"some_image_url","name":"Name 1","id":"5078c3a803ff4197dc81fbfb"},{"email":"user2@gmail.com","image":"some_image_url","name":"Name 2","id":"5078c3a803ff4197dc81fbfc"}]