2011-04-19 59 views
0

我需要把這個JSON對象:Javascript或Ruby。格式化JSON對象

[ 
    [ 
     "[email protected]" 
    ], 
    [ 
     "[email protected]" 
    ], 
    [ 
     "[email protected]" 
    ], 
    [ 
     "[email protected]" 
    ] 
] 

進入這個:

{ 
    "data": [ 
     { 
      "email": "[email protected]" 
     }, 
     { 
      "email": "[email protected]" 
     }, 
     { 
      "email": "[email protected]" 
     }, 
     { 
      "email": "[email protected]" 
     } 
] } 

這是如何完成的?

+0

的[解析紅寶石JSON字符串(可能重複http://stackoverflow.com/questions/5410682/parsing-a-json-string-in-ruby) – 2011-04-19 21:43:04

回答

1

那麼,這實際上只是一個數組的數組,但除此之外。只是通過數組的數組循環,推動相關數據到新的數組中的對象:

var my_new_object = {}; 
my_new_object['data'] = []; 

for (var i = 0; i < your_array.length; i++) { 
    my_new_object.data.push({"email": your_array[i][0]}); 
} 

Working demo

+0

你很近,但我認爲你需要:your_array [i] [0] – Jage 2011-04-19 20:22:08

+0

它非常接近。電子郵件地址周圍的[]不應該在那裏。我怎樣才能刪除它? http://pastie.org/1812997。 – 2011-04-19 20:25:05

+0

完美工作。謝謝! – 2011-04-19 20:46:07