为什么我的JSON.parse不起作用?

时间:2017-09-25 01:19:48

标签: javascript json

我在php中创建一个数组对象,然后使用json_encode对其进行编码,然后在javascript JSON.parse中回显它。我发现错误说" VM1770:1未捕获的SyntaxError:位置422的JSON中的意外标记     在JSON.parse()     在LabOne.php:19"

这是我的JSON.parse行

var JSarray = JSON.parse( '[{"name":"Carlisle Community Centre & Arena","address":"1496 Centre Road","city":" Carlisle ","phone":"905-689-7283 ","latitude":"43.3966832","longitude":"-79.9813271"},{"name":"Chedoke Twin Pad Arena","address":"91 Chedmac Dr.","city":" Hamilton ","phone":"905-546-2429 ","latitude":"43.2386207","longitude":"-79.9208443"},{"name":"Coronation Arena and Pool","address":"81 Macklin St. North","city":" Hamilton ","phone":"\t905-546-3109 \t","latitude":"43.2646431","longitude":"-79.8963275"},{"name":"Eastwood Arena","address":"111 Burlington St. East","city":" Hamilton ","phone":"\t905-546-3152 ","latitude":"43.2724438","longitude":"-79.8561951"},{"name":"Glanbrook Arena & Auditorium","address":"4300 Binbrook Road","city":" Binbrook ","phone":"\t905-692-9331 ","latitude":"43.1299662","longitude":"-79.8386732"},{"name":"Inch Park Arena & Pool","address":"400 Queensdale Ave.","city":" Hamilton ","phone":"\t905-546-4922 ","latitude":"43.2374030","longitude":"-79.8591217"},{"name":"Lawfield Arena","address":"150 Folkstone Ave.","city":" Hamilton ","phone":"905-546-4923 ","latitude":"43.2150052","longitude":"-79.8510790"},{"name":"Market Street (J.L. Grightmire) Arena","address":"35 Market St. South","city":" Dundas ","phone":"905-540-6678 ","latitude":"43.2660272","longitude":"-79.9635777"},{"name":"Mohawk 4 Ice Centre","address":"710 Mountain Brow Blvd.","city":" Hamilton \t","phone":"\t905-318-5111 ","latitude":"43.2094632","longitude":"-79.8171705"},{"name":"Morgan Firestone Arena","address":"385 Jerseyville Road West","city":" Ancaster ","phone":"\t905-546-3769 ","latitude":"43.2179937","longitude":"-80.0075459"},{"name":"Mountain (Dave Andreychuk) Arena","address":"25 Hester St.","city":" Hamilton ","phone":"\t905-546-4938 ","latitude":"43.2255668","longitude":"-79.8809353"},{"name":"North Wentworth Twin-Pad Arena","address":"27 Hwy 5","city":" Flamborough ","phone":"\t905-689-6666 \t","latitude":"43.3128542","longitude":"-79.9222993"},{"name":"Olympic Arena","address":"70 Olympic Dr.","city":" Dundas ","phone":"\t905-540-6686 ","latitude":"43.2728720","longitude":"-79.9342542"},{"name":"Parkdale (Pat Quinn) Arena and Pool","address":"1770 Main St. East","city":" Hamilton ","phone":"\t905-546-4785 ","latitude":"43.2365765","longitude":"-79.7937876"},{"name":"Rosedale Arena and Pool","address":"100 Greenhill Ave.","city":" Hamilton ","phone":"\t905-546-4805 ","latitude":"43.2215920","longitude":"-79.8144628"},{"name":"Saltfleet Arena","address":"24 Sherwood Park Road","city":" Stoney Creek ","phone":"\t905-643-3883 ","latitude":"43.2179779","longitude":"-79.7048414"},{"name":"Scott Park Arena","address":"876 Cannon St. East","city":" Hamilton ","phone":"\t905-546-4919 ","latitude":"43.2502045","longitude":"-79.8304338"},{"name":"Spring Valley Arena","address":"29 Orchard Dr.","city":" Ancaster","phone":"905-648-4404 ","latitude":"43.2169996","longitude":"-79.9981245"},{"name":"Stoney Creek Arena","address":"37 King St. West","city":" Stoney Creek ","phone":"\t905-662-2426 ","latitude":"43.2165957","longitude":"-79.7640822"},{"name":"Valley Park Arena & Rec Centre","address":"970 Paramount Dr.","city":" Stoney Creek ","phone":"905-573-3600 ","latitude":"43.1930991","longitude":"-79.7978639"}]' );

显然,它在元素的电话字段失败,名称字段为"加冕区和游泳池"。

我已经用JSONLint检查了json字符串,但它说json字符串没问题。

2 个答案:

答案 0 :(得分:2)

问题在于,您直接在javascript代码中编写此JSON字符串,escape characters在通过parse方法之前进行了转换。

如果它不是来自js字符串,而是来自 .json 文件,或来自AJAX请求,那就可以了。

但是这里\t直接用js转换为 Tabulation (U + 0009)字符。

为避免这种情况,您必须

  • 在编写js文件时将转义字符的反斜杠加倍(\\t),
  • 或者直接编写解析的对象表示法,因为在JS脚本中将JS对象编写为JSON字符串是没有意义的。 (var JSarray = [{name: ...

答案 1 :(得分:0)

问题在于您在电话号码中使用\t

删除所有这些修复了问题:

var JSarray = JSON.parse('[{"name":"Carlisle Community Centre & Arena","address":"1496 Centre Road","city":" Carlisle ","phone":"905-689-7283 ","latitude":"43.3966832","longitude":"-79.9813271"},{"name":"Chedoke Twin Pad Arena","address":"91 Chedmac Dr.","city":" Hamilton ","phone":"905-546-2429 ","latitude":"43.2386207","longitude":"-79.9208443"},{"name":"Coronation Arena and Pool","address":"81 Macklin St. North","city":" Hamilton ","phone":"905-546-3109 ","latitude":"43.2646431","longitude":"-79.8963275"},{"name":"Eastwood Arena","address":"111 Burlington St. East","city":" Hamilton ","phone":"905-546-3152 ","latitude":"43.2724438","longitude":"-79.8561951"},{"name":"Glanbrook Arena & Auditorium","address":"4300 Binbrook Road","city":" Binbrook ","phone":"905-692-9331 ","latitude":"43.1299662","longitude":"-79.8386732"},{"name":"Inch Park Arena & Pool","address":"400 Queensdale Ave.","city":" Hamilton ","phone":"905-546-4922 ","latitude":"43.2374030","longitude":"-79.8591217"},{"name":"Lawfield Arena","address":"150 Folkstone Ave.","city":" Hamilton ","phone":"905-546-4923 ","latitude":"43.2150052","longitude":"-79.8510790"},{"name":"Market Street (J.L. Grightmire) Arena","address":"35 Market St. South","city":" Dundas ","phone":"905-540-6678 ","latitude":"43.2660272","longitude":"-79.9635777"},{"name":"Mohawk 4 Ice Centre","address":"710 Mountain Brow Blvd.","city":" Hamilton ","phone":"905-318-5111 ","latitude":"43.2094632","longitude":"-79.8171705"},{"name":"Morgan Firestone Arena","address":"385 Jerseyville Road West","city":" Ancaster ","phone":"905-546-3769 ","latitude":"43.2179937","longitude":"-80.0075459"},{"name":"Mountain (Dave Andreychuk) Arena","address":"25 Hester St.","city":" Hamilton ","phone":"905-546-4938 ","latitude":"43.2255668","longitude":"-79.8809353"},{"name":"North Wentworth Twin-Pad Arena","address":"27 Hwy 5","city":" Flamborough ","phone":"905-689-6666 ","latitude":"43.3128542","longitude":"-79.9222993"},{"name":"Olympic Arena","address":"70 Olympic Dr.","city":" Dundas ","phone":"905-540-6686 ","latitude":"43.2728720","longitude":"-79.9342542"},{"name":"Parkdale (Pat Quinn) Arena and Pool","address":"1770 Main St. East","city":" Hamilton ","phone":"905-546-4785 ","latitude":"43.2365765","longitude":"-79.7937876"},{"name":"Rosedale Arena and Pool","address":"100 Greenhill Ave.","city":" Hamilton ","phone":"905-546-4805 ","latitude":"43.2215920","longitude":"-79.8144628"},{"name":"Saltfleet Arena","address":"24 Sherwood Park Road","city":" Stoney Creek ","phone":"905-643-3883 ","latitude":"43.2179779","longitude":"-79.7048414"},{"name":"Scott Park Arena","address":"876 Cannon St. East","city":" Hamilton ","phone":"905-546-4919 ","latitude":"43.2502045","longitude":"-79.8304338"},{"name":"Spring Valley Arena","address":"29 Orchard Dr.","city":" Ancaster","phone":"905-648-4404 ","latitude":"43.2169996","longitude":"-79.9981245"},{"name":"Stoney Creek Arena","address":"37 King St. West","city":" Stoney Creek ","phone":"905-662-2426 ","latitude":"43.2165957","longitude":"-79.7640822"},{"name":"Valley Park Arena & Rec Centre","address":"970 Paramount Dr.","city":" Stoney Creek ","phone":"905-573-3600 ","latitude":"43.1930991","longitude":"-79.7978639"}]');
console.log(JSarray);

假设您以包含\t个实例的格式收到JSON,您只需使用.replace(/\t/g, '')删除它们即可。请注意,这需要在 解析JSON之前完成,因此,为了便于说明,我在以下示例中分离了以下步骤:

var JSarray = '[{"name":"Carlisle Community Centre & Arena","address":"1496 Centre Road","city":" Carlisle ","phone":"905-689-7283 ","latitude":"43.3966832","longitude":"-79.9813271"},{"name":"Chedoke Twin Pad Arena","address":"91 Chedmac Dr.","city":" Hamilton ","phone":"905-546-2429 ","latitude":"43.2386207","longitude":"-79.9208443"},{"name":"Coronation Arena and Pool","address":"81 Macklin St. North","city":" Hamilton ","phone":"\t905-546-3109 \t","latitude":"43.2646431","longitude":"-79.8963275"},{"name":"Eastwood Arena","address":"111 Burlington St. East","city":" Hamilton ","phone":"\t905-546-3152 ","latitude":"43.2724438","longitude":"-79.8561951"},{"name":"Glanbrook Arena & Auditorium","address":"4300 Binbrook Road","city":" Binbrook ","phone":"\t905-692-9331 ","latitude":"43.1299662","longitude":"-79.8386732"},{"name":"Inch Park Arena & Pool","address":"400 Queensdale Ave.","city":" Hamilton ","phone":"\t905-546-4922 ","latitude":"43.2374030","longitude":"-79.8591217"},{"name":"Lawfield Arena","address":"150 Folkstone Ave.","city":" Hamilton ","phone":"905-546-4923 ","latitude":"43.2150052","longitude":"-79.8510790"},{"name":"Market Street (J.L. Grightmire) Arena","address":"35 Market St. South","city":" Dundas ","phone":"905-540-6678 ","latitude":"43.2660272","longitude":"-79.9635777"},{"name":"Mohawk 4 Ice Centre","address":"710 Mountain Brow Blvd.","city":" Hamilton \t","phone":"\t905-318-5111 ","latitude":"43.2094632","longitude":"-79.8171705"},{"name":"Morgan Firestone Arena","address":"385 Jerseyville Road West","city":" Ancaster ","phone":"\t905-546-3769 ","latitude":"43.2179937","longitude":"-80.0075459"},{"name":"Mountain (Dave Andreychuk) Arena","address":"25 Hester St.","city":" Hamilton ","phone":"\t905-546-4938 ","latitude":"43.2255668","longitude":"-79.8809353"},{"name":"North Wentworth Twin-Pad Arena","address":"27 Hwy 5","city":" Flamborough ","phone":"\t905-689-6666 \t","latitude":"43.3128542","longitude":"-79.9222993"},{"name":"Olympic Arena","address":"70 Olympic Dr.","city":" Dundas ","phone":"\t905-540-6686 ","latitude":"43.2728720","longitude":"-79.9342542"},{"name":"Parkdale (Pat Quinn) Arena and Pool","address":"1770 Main St. East","city":" Hamilton ","phone":"\t905-546-4785 ","latitude":"43.2365765","longitude":"-79.7937876"},{"name":"Rosedale Arena and Pool","address":"100 Greenhill Ave.","city":" Hamilton ","phone":"\t905-546-4805 ","latitude":"43.2215920","longitude":"-79.8144628"},{"name":"Saltfleet Arena","address":"24 Sherwood Park Road","city":" Stoney Creek ","phone":"\t905-643-3883 ","latitude":"43.2179779","longitude":"-79.7048414"},{"name":"Scott Park Arena","address":"876 Cannon St. East","city":" Hamilton ","phone":"\t905-546-4919 ","latitude":"43.2502045","longitude":"-79.8304338"},{"name":"Spring Valley Arena","address":"29 Orchard Dr.","city":" Ancaster","phone":"905-648-4404 ","latitude":"43.2169996","longitude":"-79.9981245"},{"name":"Stoney Creek Arena","address":"37 King St. West","city":" Stoney Creek ","phone":"\t905-662-2426 ","latitude":"43.2165957","longitude":"-79.7640822"},{"name":"Valley Park Arena & Rec Centre","address":"970 Paramount Dr.","city":" Stoney Creek ","phone":"905-573-3600 ","latitude":"43.1930991","longitude":"-79.7978639"}]';
JSarray = JSarray.replace(/\t/g, '');
Jsarray = JSON.parse(JSarray);
console.log(JSarray);

希望这有帮助! :)