当前位置: 首页 > 面试题库 >

JSON.parse()意外令牌错误

戚默
2023-03-14
问题内容

unexpected token尝试解析此JSON字符串时出现错误

$scope.feeds = JSON.parse('[{"id":"212216417436_10152811286407437","from":{ "category":"Movie","name":"The Lord of the Rings Trilogy","id":"212216417436"},"story":"The Lord of the Rings Trilogy shared The Hobbit\'s photo.","picture":"https://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-xaf1/v/t1.0-9/s130x130/1912397_736719376363670_7288336626443028501_n.jpg?oh=a7e83093f61fafa7909ff84263f989e4&oe=54B4481D&__gda__=1420472696_dbd6462629a00e218c849c5ed7b49c44","link":"https://www.facebook.com/TheHobbitMovie/photos/a.291674244201521.63650.160617097307237/736719376363670/?type=1","name":"Timeline Photos","caption":"\"I have the only right.\" \n\nWhat is your favorite Thorin quote from #TheHobbit?","properties":[  {  "name":"By","text":"The Hobbit","href":"https://www.facebook.com/TheHobbitMovie?ref=stream"}],"icon":"https://fbstatic-a.akamaihd.net/rsrc.php/v2/yD/r/aS8ecmYRys0.gif","actions":[  { "name":"Comment","link":"https://www.facebook.com/212216417436/posts/10152811286407437"},{ "name":"Like","link":"https://www.facebook.com/212216417436/posts/10152811286407437"}],"privacy":{ "value":""},"type":"photo","status_type":"shared_story","object_id":"736719376363670","application":{ "name":"Photos","id":"2305272732"},"created_time":"2014-10-12T21:35:41+0000","updated_time":"2014-10-12T21:37:17+0000"},{  "id":"21785951839_10152794856921840","from":{ "category":"App page","name":"9GAG","id":"21785951839"},"message":"Must NOT let my girlfriend see this!\nhttp://9gag.tv/p/a9OrlY?ref=fbl9\n\nWatch more awesome videos on your phone:\nAndroid ► http://goo.gl/Rbi9FP\niPhone/iPad ► http://goo.gl/NFMm9p","picture":"https://fbexternal-a.akamaihd.net/safe_image.php?d=AQCee84KqIBIdnIX&w=158&h=158&url=http%3A%2F%2Fd1d4324mp0stc8.cloudfront.net%2Fmedia%2Fphoto%2FpPgXmO5QJ_600w_v1.jpg","link":"http://9gag.tv/p/a9OrlY?ref=fbl9","name":"This \"Marry Me\" Lip-Sync Proposal Is So Touching And Amazing | 9GAG.tv","caption":"9gag.tv","description":"This sets the bar pretty high!","icon":"https://fbstatic-a.akamaihd.net/rsrc.php/v2/yD/r/aS8ecmYRys0.gif","actions":[  { "name":"Comment","link":"https://www.facebook.com/21785951839/posts/10152794856921840"},{ "name":"Like","link":"https://www.facebook.com/21785951839/posts/10152794856921840"}],"privacy":{ "value":""},"type":"link","status_type":"shared_story","created_time":"2014-10-12T21:15:00+0000","updated_time":"2014-10-12T21:15:46+0000","shares":{  "count":160}}]');

扩展的字符串对象:

{
   "id":"212216417436_10152811286407437",
   "from":{
      "category":"Movie",
      "name":"The Lord of the Rings Trilogy",
      "id":"212216417436"
   },
   "story":"The Lord of the Rings Trilogy shared The Hobbit\'s photo.",
   "picture":"https://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-xaf1/v/t1.0-9/s130x130/1912397_736719376363670_7288336626443028501_n.jpg?oh=a7e83093f61fafa7909ff84263f989e4&oe=54B4481D&__gda__=1420472696_dbd6462629a00e218c849c5ed7b49c44",
   "link":"https://www.facebook.com/TheHobbitMovie/photos/a.291674244201521.63650.160617097307237/736719376363670/?type=1",
   "name":"Timeline Photos",
   "caption":"\"I have the only right.\" \n\nWhat is your favorite Thorin quote from #TheHobbit?",
   "properties":[
      {
         "name":"By",
         "text":"The Hobbit",
         "href":"https://www.facebook.com/TheHobbitMovie?ref=stream"
      }
   ],
   "icon":"https://fbstatic-a.akamaihd.net/rsrc.php/v2/yD/r/aS8ecmYRys0.gif",
   "actions":[
      {
         "name":"Comment",
         "link":"https://www.facebook.com/212216417436/posts/10152811286407437"
      },
      {
         "name":"Like",
         "link":"https://www.facebook.com/212216417436/posts/10152811286407437"
      }
   ],
   "privacy":{
      "value":""
   },
   "type":"photo",
   "status_type":"shared_story",
   "object_id":"736719376363670",
   "application":{
      "name":"Photos",
      "id":"2305272732"
   },
   "created_time":"2014-10-12T21:35:41+0000",
   "updated_time":"2014-10-12T21:37:17+0000"
},
{
   "id":"21785951839_10152794856921840",
   "from":{
      "category":"App page",
      "name":"9GAG",
      "id":"21785951839"
   },
   "message":"Must NOT let my girlfriend see this!\nhttp://9gag.tv/p/a9OrlY?ref=fbl9\n\nWatch more awesome videos on your phone:\nAndroid ► http://goo.gl/Rbi9FP\niPhone/iPad ► http://goo.gl/NFMm9p",
   "picture":"https://fbexternal-a.akamaihd.net/safe_image.php?d=AQCee84KqIBIdnIX&w=158&h=158&url=http%3A%2F%2Fd1d4324mp0stc8.cloudfront.net%2Fmedia%2Fphoto%2FpPgXmO5QJ_600w_v1.jpg",
   "link":"http://9gag.tv/p/a9OrlY?ref=fbl9",
   "name":"This \"Marry Me\" Lip-Sync Proposal Is So Touching And Amazing | 9GAG.tv",
   "caption":"9gag.tv",
   "description":"This sets the bar pretty high!",
   "icon":"https://fbstatic-a.akamaihd.net/rsrc.php/v2/yD/r/aS8ecmYRys0.gif",
   "actions":[
      {
         "name":"Comment",
         "link":"https://www.facebook.com/21785951839/posts/10152794856921840"
      },
      {
         "name":"Like",
         "link":"https://www.facebook.com/21785951839/posts/10152794856921840"
      }
   ],
   "privacy":{
      "value":""
   },
   "type":"link",
   "status_type":"shared_story",
   "created_time":"2014-10-12T21:15:00+0000",
   "updated_time":"2014-10-12T21:15:46+0000",
   "shares":{
      "count":160
   }
}

问题答案:

验证您的json字符串。如果可以,请尝试以下操作:

var jsonStr="your json string";
var json=JSON.stringify(jsonStr);
json=JSON.parse(json)


 类似资料:
  • 问题内容: 为什么每次我做时:- 它只是解析很好,但是当我这样做时:- 它给我一个错误,说:- 问题答案: 您要它解析JSON文本(不是)。那是无效的JSON,字符串必须用双引号引起来。 如果要与第一个示例等效:

  • 问题内容: 我从网络服务返回的JSON遇到问题。看起来JSON缺少引号,但是当我在JSON中添加引号时,出现错误。这是错误消息:’Uncaught SyntaxError:意外的标记o。当我将字符串记录到控制台时:[对象对象],[对象对象] 这是一些模拟错误的示例代码: 这是相同的代码,在字符串两边用单引号引起来。有用 但是,当我尝试在字符串中添加引号时,就像我在实际代码中似乎需要的那样,它失败了

  • 问题内容: 我尝试了以下简单的JavaScript代码: 例如,在Chrome控制台中,这将返回 SyntaxError:意外令牌: 我在JSONLint上尝试了JSON ,它是有效的。 您看到错误了吗? 问题答案: FWIW,改为使用。比。

  • 问题内容: 我收到此错误: JSON.parse:意外字符 当我在firebug中运行以下语句时: 为什么会这样呢?JSON字符串对我来说似乎是正确的,我也使用JSHint对其进行了测试。在上述情况下,传递的对象是服务器响应,其内容类型设置为 问题答案: 您不是在解析字符串,而是在解析一个已经解析的对象:)

  • 问题内容: 是什么导致第三行出现此错误? 打开控制台以查看错误 问题答案: 是一个对象。(从对象文字创建) 用于将包含JSON表示法的 字符串 转换为Javascript对象。 您的代码将对象转换为字符串(通过调用),以尝试将其解析为JSON文本。 默认返回,这是无效的JSON;因此错误。

  • 应用程序。js公司 在执行上述代码时,我得到以下错误。 当我将鼠标悬停在线上方时,会出现这个错误 在我的VS代码中。 当我使用node运行代码时,即在上。我在终端中收到以下错误: 我做错了什么?我希望最后的obj的值是{1:,2:,3:}