JOSN.parse功能不正常

时间:2016-09-28 14:07:20

标签: javascript jquery json

您好我试图解析简单的json(字符串),但我收到了错误。 这是我需要得到的对象

{
"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);




2 个答案:

答案 0 :(得分:0)

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

答案 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方法,或者您使用的字符串是否在某些时候包含单引号或双引号