我收到有效JSON的JSON.parse错误 [英] I am receiving a JSON.parse error on valid JSON

查看:202
本文介绍了我收到有效JSON的JSON.parse错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在解析一些JSON.我的JSON是有效的,因为当我通过JSONLint运行它时,会得到绿色标签有效JSON",但是由于某些原因,我仍然无法通过Angular Controller解析JSON.

I am parsing some JSON. My JSON is valid, because when I run it through JSONLint, I get the green label saying Valid JSON, yet for some reason I still cannot parse the JSON via my Angular Controller.

JSON代码.

JSON code can be found here.

控制器代码:

savvyApp.controller('ProductsCtrl', function($scope) {

    var apiJSONResult = '<linked json here>';   

    $scope.apiResult = JSON.parse(apiJSONResult);

});

推荐答案

JSON.parse字符串与引号

那应该是答案.简而言之,您不能只复制粘贴JSON并用单引号引起来并期望它能正常工作.您还需要确保反斜杠已编码.

That should be the answer. In short you cannot just copy paste JSON and quote it in single quotes and expect it to work. You would also need to make sure the backslashes are encoded.

因此JSON 实际上是有效的.但是,如果您将其复制粘贴到js文件中,则它将不再有效.它与javascript如何在字符串中编码反斜杠有关.

So the JSON is in fact valid. But it is no longer valid if you copy paste it into a js file. It has to do with how javascript encodes backslashes in strings.

这篇关于我收到有效JSON的JSON.parse错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆