Angular.js格式化json数据

时间:2015-03-28 12:57:16

标签: javascript json angularjs

"[{rest_id:"2", cate_id:"4", rest_name:"Samraat Curry Hut", adress:"6275 Jarvis Ave, Newark, CA 94560", city:"Newark", state:"New Jersey", country:"US", email_id:"", rest_url:"Samraat-Curry-Hut", phone_no:"(510) 745-7000", rest_image:"http://www.mealhi5.com/images/restaurant/Samraat-Curry-Hut.jpg"}]"

top array我的结果,但我需要的底部数组添加双引号键 rest_id到“rest_id”所有键添加双引号(“”) 我需要的数组

这个Angular JS 第一个JSON是我的输入和输出第二个数组

[{"rest_id":"2", "cate_id":"4", "rest_name":"Samraat Curry Hut", "adress":"6275 Jarvis Ave, Newark, CA 94560", "city":"Newark", "state":"New Jersey", "country":"US", "email_id":"", "rest_url":"Samraat-Curry-Hut", "phone_no":"(510) 745-7000", "rest_image":"http://www.mealhi5.com/images/restaurant/Samraat-Curry-Hut.jpg"}]

2 个答案:

答案 0 :(得分:1)

查看JSON.parse(如果您想将其用作JSON对象)

var foo = JSON.parse("[{rest_id:"2", ...}]");

答案 1 :(得分:0)

在这种情况下,你可以做一件事使用正则表达式: -

var a= JSON.stringify([{"rest_id":"2", "cate_id":"4", "rest_name":"Samraat Curry Hut", "adress":"6275 Jarvis Ave, Newark, CA 94560", "city":"Newark", "state":"New Jersey", "country":"US", "email_id":"", "rest_url":"Samraat-Curry-Hut", "phone_no":"(510) 745-7000", "rest_image":"http://www.mealhi5.com/images/restaurant/Samraat-Curry-Hut.jpg"}]);

a=a.replace(/"(\w+)"\s*:/g, '$1:');

Fiddle