使用jquery读取json响应

时间:2013-09-02 17:01:50

标签: javascript jquery json

我想阅读下面的json数组。

[{"bus_code":"1103030017","bus_name":"Tony\u0027s Sports Bar \u0026 Grill","first_name":"","last_name":"","email":"tonyssports@bellsouth.net","phone":"(770) 552-2233","street_address":"8610 Roswell Rd","city":"Sandy Springs","state":"GA","zip_code":"30350","website":"www.tonyssportsbar.com/","open_hours":"Mon-Fri 11am-2am, Sat-Sun 12pm-2am","features":"Live entertainment, TVs, billiards, Wi-Fi. ","type":"Sports Bar, Restaurant","coords":"33.989659, -84.351690"}]

使用此代码但未取得成功

info = jQuery.parseJSON(data);

alert(info.bus_code);

但没有获得成功。

请建议我。

3 个答案:

答案 0 :(得分:4)

尝试:

alert(info[0].bus_code);

(对象在数组中)。

答案 1 :(得分:2)

您的数据已采用JSON格式。所以不需要使用jQuery.parseJSON(data)及其数组,因此请使用data[0].bus_code

 var data = [{"bus_code":"1103030017","bus_name":"Tony\u0027s Sports Bar \u0026 Grill","first_name":"","last_name":"","email":"tonyssports@bellsouth.net","phone":"(770) 552-2233","street_address":"8610 Roswell Rd","city":"Sandy Springs","state":"GA","zip_code":"30350","website":"www.tonyssportsbar.com/","open_hours":"Mon-Fri 11am-2am, Sat-Sun 12pm-2am","features":"Live entertainment, TVs, billiards, Wi-Fi. ","type":"Sports Bar, Restaurant","coords":"33.989659, -84.351690"}];
 console.log(data[0].bus_code);

Fiddle

根据@Boaz如果您输入的是字符串,则必须使用jQuery.parseJSON

答案 2 :(得分:2)

试试这个。

alert(info[0]['bus_code']);

or

alert(info[0].bus_code);

<强> See Demo