【问题标题】:JOSN.parse function not working correctJSON.parse 函数无法正常工作
【发布时间】:2016-09-28 14:07:19
【问题描述】:

您好,我正在尝试解析简单的 json(string),但出现错误。 这是我需要得到的对象

{
"heading" : "The movies",
"box5" : "Click on icon to add text.",
"box1" : "At the movies,you can watch for an hour. ",
"box2" : "You can go eat popcorn,candy,and soda. ",
"box7" : "At the movies you will want to go again. ",
"box3" : "At the movies ,you can play games.",
"box4" : "you can go and have fun with your friends and family.And get to spent time with them.========                ",
"box6" : "Click on icon to add text."
};

.========

我认为这是常规字符串字符

当我删除 ======== 时,我得到了结果

var st = '{"heading": "The movies","box5": "Click on icon to add text.","box1": "At the movies,you can watch for an hour. ","box2": "You can go eat popcorn,candy,and soda. ","box7": "At the movies you will want to go again. ","box3": "At the movies ,you can play games.","box4": "you can go and have fun with your friends and family.And get to spent time with them.","box6": "Click on icon to add text."}';
console.log(st);

var res = JSON.parse(st);

console.log('res: ' + res.heading);

包括

var st = '{"heading": "The movies","box5": "Click on icon to add text.","box1": "At the movies,you can watch for an hour. ","box2": "You can go eat popcorn,candy,and soda. ","box7": "At the movies you will want to go again. ","box3": "At the movies ,you can play games.","box4": "you can go and have fun with your friends and family.And get to spent time with them.========	               ","box6": "Click on icon to add text."}';

console.log(st);

var res = JSON.parse(st);

console.log('res' + res.heading);

【问题讨论】:

  • 我没有收到任何错误。
  • 请在一个代码块中显示您的 JSON(只是您的 JSON),并在另一个代码块中显示您的代码。现在有一个大杂烩,包括; 似乎是您的 JSON(这将是无效的),因此很难为您提供帮助。
  • 如果我们假设您的 JSON 是从 {} 的部分并且不包括 ;,那么 JSON 就可以了。
  • 等号后面的空白字符串是什么?是一连串的回车吗?这会破坏 JSON
  • @abosancic:不要试图把它放在评论中,使用问题上的“编辑”链接。但同样,它工作得很好,所以这显然不是您遇到问题的实际 JSON。

标签: javascript jquery json


【解决方案1】:

我发现了问题所在。字符串“.========”包含选项卡,这是这里的问题。用空间解析替换选项卡后工作正常。

【讨论】:

    【解决方案2】:

    试试这个字符串

    var obj = '{"heading":"The movies","box5":"Click on icon to add text.","box1":"At the movies,you can watch for an hour. ","box2":"You can go eat popcorn,candy,and soda. ","box7":"At the movies you will want to go again. ","box3":"At the movies ,you can play games.","box4":"you can go and have fun with your friends and family.And get to spent time with them.========                ","box6":"Click on icon to add text."}'
    
    alert(JSON.parse(obj)); //works for me

    但它对你有用,但它不适用于你的页面尝试检查是否有东西覆盖了 JSON.parse 方法,或者你使用的字符串在某些时候是否包含单引号或双引号

    【讨论】:

      猜你喜欢
      • 2015-09-01
      • 2020-06-19
      • 2017-12-15
      • 2013-03-27
      • 2012-08-15
      • 2013-11-05
      • 1970-01-01
      • 2022-01-11
      • 1970-01-01
      相关资源
      最近更新 更多