我有一个具有以下内容的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做到这一点?
谢谢。
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中的名称/值对中没有意义,因此与您想要的结果相同。