2016-09-28 86 views
-5

嗨我想解析简单的JSON(字符串),但我得到错误。 这是目标,我需要得到JOSN.parse函数无法正常工作

{ 
"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.======== \t    ","box6": "Click on icon to add text."}'; 
 

 
console.log(st); 
 

 
var res = JSON.parse(st); 
 

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

+0

我没有收到任何错误。 – Xufox

+0

请在一个代码块中显示您的JSON(**,只是**您的JSON),并在另一个代码块中显示您的代码。现在有什么是一种混杂的混搭,包括看起来像你的JSON(这将是无效的),所以它很难帮助你。 –

+0

如果我们假设你的JSON是'{'到'}'的一部分而不包括';',那么JSON就没问题。 –

回答

0

我发现问题所在。字符串“。========”包含标签,这是这里的问题。替换标签与空间解析工作正常。

-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方法,或者您正在使用的字符串在某个点包含单引号或双引号