2016-09-28 85 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方法,或者您正在使用的字符串在某個點包含單引號或雙引號