2012-11-15 100 views
32

我有以下內容的JSON對象:如何重命名JSON關鍵

[ 
    { 
    "_id":"5078c3a803ff4197dc81fbfb", 
    "email":"[email protected]", 
    "image":"some_image_url", 
    "name":"Name 1" 
    }, 
    { 
    "_id":"5078c3a803ff4197dc81fbfc", 
    "email":"[email protected]", 
    "image":"some_image_url", 
    "name":"Name 2" 
    } 
] 

我想改變「_id」鍵,「身份證」,所以它會成爲

[ 
    { 
    "id":"5078c3a803ff4197dc81fbfb", 
    "email":"[email protected]", 
    "image":"some_image_url", 
    "name":"Name 1" 
    }, 
    { 
    "id":"5078c3a803ff4197dc81fbfc", 
    "email":"[email protected]", 
    "image":"some_image_url", 
    "name":"Name 2" 
    } 
] 

如何我會用JavaScript,jQuery或Ruby,Rails來做這件事嗎?

謝謝。

+3

解析JSON,相應地修改所得到的數據結構,並且將其轉換回JSON。指針:http://stackoverflow.com/questions/4935632/how-to-parse-json-in-javascript,http://stackoverflow.com/questions/1826727/how-do-i-parse-json-with-的Ruby-on-軌,http://stackoverflow.com/questions/2295496/convert-javascript-array-to-json,http://stackoverflow.com/questions/3183786/how-to-convert-a-ruby-哈希對象到JSON。 –

回答

62
var json = '[{"_id":"5078c3a803ff4197dc81fbfb","email":"[email protected]","image":"some_image_url","name":"Name 1"},{"_id":"5078c3a803ff4197dc81fbfc","email":"[email protected]","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":"[email protected]","image":"some_image_url","name":"Name 1","id":"5078c3a803ff4197dc81fbfb"}] 

由於訂單沒有在JSON名稱/值對的意思,這是一樣的你想要的結果。

+3

如果我想將更改應用於json中的所有_id,該怎麼辦? – truongnm

+1

@truongnm循環它 –

+0

此外,如果你想與空格鍵就可以添加像這樣的obj [「我的身份證」] = obj.id – catchiecop

21

在這種情況下,它將是最容易使用字符串替換。序列化JSON將無法正常工作,因爲_id將成爲對象的屬性名稱,更改屬性名稱不是一件簡單的任務(至少在大多數語言中不是這樣,它在JavaScript中並不那麼糟糕)。相反只是做;

 jsonString = jsonString.replace("\"_id\":", "\"id\":"); 
10

正如evanmcdonnal提到的,最簡單的解決方案是處理此作爲字符串而不是JSON,

var json = [{"_id":"5078c3a803ff4197dc81fbfb","email":"[email protected]","image":"some_image_url","name":"Name 1"},{"_id":"5078c3a803ff4197dc81fbfc","email":"[email protected]","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格式。但我使用splitjoin而不是replace,因爲replace將只替換第一次出現的字符串。

+1

非常感謝陌生人先生,這真的幫助我。 –

1

如果你想重命名所有的事件的一些關鍵你可以使用g選項的正則表達式。例如:

var json = [{"_id":"1","email":"[email protected]","image":"some_image_url","name":"Name 1"},{"_id":"2","email":"[email protected]","image":"some_image_url","name":"Name 2"}]; 

str = JSON.stringify(json); 

現在我們在str中使用字符串格式的json。以 「身份證」 使用

替換 「_id」 的所有出現的正則表達式與選項:

str = str.replace(/\"_id\":/g, "\"id\":"); 

,並返回到JSON格式:

json = JSON.parse(str); 

現在我們有我們的JSON想要的密鑰名稱。

+0

如果有一個值的任何地方包含要更換的鍵名,因爲它將取代,以及這將無法正常工作。 – Carasel

+0

好點,但邊緣情況。更換文字時一定要小心 – rafraph

0

如果要動態做到這一點,例如,你必須要應用作爲重點,以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++; 
    }) 
}) 

你得到JSON陣列將是這樣的:

[ 
    { 
    "id": 1, 
    "name": "Asna", 
    "Address": "NY", 
    "Phone": 123 
    }, 
    { 
    "id": 2, 
    "name": "Euphoria", 
    "Address": "Monaco", 
    "Phone": 124 
    }, 
    { 
    "id": 3, 
    "name": "Ahmed", 
    "Address": "Mumbai", 
    "Phone": 125 
    } 
]