Json解析错误:JSON.parse:字符串文字中的错误控制字符

时间:2012-03-13 13:29:07

标签: jquery jquery-mobile

我使用移动jquery为移动网站创建了Web服务,它将返回json字符串。 当我开始使用jQuery.parseJSON方法解析这个json时,有时它工作正常,但在某些情况下,它会产生如下错误: JSON.parse: bad control character in string literal

我已经在jsonlint.com上验证了这个json,它给出了一个有效的json消息。

Follwoing是我的json字符串。

[{"ID":"468438","Text":"<a target='_blank' class='msg_links' href='http://bit.ly/jJa131' >http://bit.ly/jJa131</a> sdfdsfasd <br> <br> shankar (shankar) on Ogoing Small Business Social Network <br> <br> IT company ","CreatedText":"Jan 20 2012 6:01AM","UserID":"535","UserName":"shankar","FullName":"shankar","UpCategoryID":"8","UpCategoryName":"Deals","CategoryName":"Education & child care","IsRecommend":"False","RecommendUser":"","RecommendRating":"0","RecommendId":"0","LikedCount":"0","CommentsCount":"0","TotalRowCount":"1011","Attachment":"","RealFilename":"","Source":"","ImageURL":"http://ogoing.rigelnetworks.com/Images/Thumb/music_arts_icon_large_634666567215996287.jpg"},{"ID":"468437","Text":"<a target='_blank' class='msg_links' href='http://bit.ly/jJa131' >http://bit.ly/jJa131</a> asdfasdfasdfasdfasdf <br> shankar (shankar) on Ogoing Small Business Social Network <br> IT company ","CreatedText":"Jan 20 2012 5:35AM","UserID":"535","UserName":"shankar","FullName":"shankar","UpCategoryID":"8","UpCategoryName":"Deals","CategoryName":"Education & child care","IsRecommend":"False","RecommendUser":"","RecommendRating":"0","RecommendId":"0","LikedCount":"0","CommentsCount":"0","TotalRowCount":"1011","Attachment":"","RealFilename":"","Source":"","ImageURL":"http://ogoing.rigelnetworks.com/Images/Thumb/music_arts_icon_large_634666567215996287.jpg"},{"ID":"468434","Text":"asdfasdfasdf asdfasdfsadfasdfa ","CreatedText":"Jan 20 2012 4:55AM","UserID":"535","UserName":"shankar","FullName":"shankar","UpCategoryID":"8","UpCategoryName":"Deals","CategoryName":"Education & child care","IsRecommend":"False","RecommendUser":"","RecommendRating":"0","RecommendId":"0","LikedCount":"0","CommentsCount":"0","TotalRowCount":"1011","Attachment":"","RealFilename":"","Source":"","ImageURL":"http://ogoing.rigelnetworks.com/Images/Thumb/music_arts_icon_large_634666567215996287.jpg"},{"ID":"468433","Text":"asdfasdfasdf asdfasdf ","CreatedText":"Jan 20 2012 4:55AM","UserID":"535","UserName":"shankar","FullName":"shankar","UpCategoryID":"8","UpCategoryName":"Deals","CategoryName":"Education & child care","IsRecommend":"False","RecommendUser":"","RecommendRating":"0","RecommendId":"0","LikedCount":"0","CommentsCount":"0","TotalRowCount":"1011","Attachment":"","RealFilename":"","Source":"","ImageURL":"http://ogoing.rigelnetworks.com/Images/Thumb/music_arts_icon_large_634666567215996287.jpg"},{"ID":"468432","Text":"asdfasdfasdf ","CreatedText":"Jan 20 2012 4:54AM","UserID":"535","UserName":"shankar","FullName":"shankar","UpCategoryID":"8","UpCategoryName":"Deals","CategoryName":"Education & child care","IsRecommend":"False","RecommendUser":"","RecommendRating":"0","RecommendId":"0","LikedCount":"0","CommentsCount":"0","TotalRowCount":"1011","Attachment":"","RealFilename":"","Source":"","ImageURL":"http://ogoing.rigelnetworks.com/Images/Thumb/music_arts_icon_large_634666567215996287.jpg"},{"ID":"468012","Text":"Why this ? ","CreatedText":"Dec 20 2011 12:37AM","UserID":"537","UserName":"shankar","FullName":"shankar mahadevan","UpCategoryID":"1","UpCategoryName":"General","CategoryName":"Automotive","IsRecommend":"False","RecommendUser":"","RecommendRating":"0","RecommendId":"0","LikedCount":"1","CommentsCount":"0","TotalRowCount":"1011","Attachment":"","RealFilename":"","Source":"ogoing","ImageURL":"http://ogoing.rigelnetworks.com/Images/Thumb/img634661225851958174_org.png"},{"ID":"468011","Text":"Tixkhogclycyo ","CreatedText":"Dec 20 2011 12:37AM","UserID":"537","UserName":"shankar","FullName":"shankar mahadevan","UpCategoryID":"1","UpCategoryName":"General","CategoryName":"Automotive","IsRecommend":"False","RecommendUser":"","RecommendRating":"0","RecommendId":"0","LikedCount":"0","CommentsCount":"0","TotalRowCount":"1011","Attachment":"","RealFilename":"","Source":"ogoing","ImageURL":"http://ogoing.rigelnetworks.com/Images/Thumb/img634661225851958174_org.png"},{"ID":"468010","Text":"Zgkxxkxxhhl ","CreatedText":"Dec 20 2011 12:35AM","UserID":"537","UserName":"shankar","FullName":"shankar mahadevan","UpCategoryID":"1","UpCategoryName":"General","CategoryName":"Automotive","IsRecommend":"False","RecommendUser":"","RecommendRating":"0","RecommendId":"0","LikedCount":"0","CommentsCount":"0","TotalRowCount":"1011","Attachment":"","RealFilename":"","Source":"ogoing","ImageURL":"http://ogoing.rigelnetworks.com/Images/Thumb/img634661225851958174_org.png"},{"ID":"468009","Text":"Jfzkdlufil ","CreatedText":"Dec 20 2011 12:35AM","UserID":"537","UserName":"shankar","FullName":"shankar mahadevan","UpCategoryID":"1","UpCategoryName":"General","CategoryName":"Automotive","IsRecommend":"False","RecommendUser":"","RecommendRating":"0","RecommendId":"0","LikedCount":"0","CommentsCount":"0","TotalRowCount":"1011","Attachment":"","RealFilename":"","Source":"ogoing","ImageURL":"http://ogoing.rigelnetworks.com/Images/Thumb/img634661225851958174_org.png"},{"ID":"468007","Text":"hellooooooo ","CreatedText":"Dec 20 2011 12:21AM","UserID":"537","UserName":"shankar","FullName":"shankar mahadevan","UpCategoryID":"1","UpCategoryName":"General","CategoryName":"Automotive","IsRecommend":"False","RecommendUser":"","RecommendRating":"0","RecommendId":"0","LikedCount":"0","CommentsCount":"0","TotalRowCount":"1011","Attachment":"","RealFilename":"","Source":"ogoing","ImageURL":"http://ogoing.rigelnetworks.com/Images/Thumb/img634661225851958174_org.png"}]

请给我一个解决这个问题的方法。 提前谢谢。

4 个答案:

答案 0 :(得分:12)

您可以将\n替换为\\n 例如

yourJsonText = yourJsonText.Replace("\n","\\n");

但是如果你使用asp.net,最好用

编码你的json字符串

yourJsonText = HttpUtility.JavaScriptStringEncode(yourJsonText)

答案 1 :(得分:4)

这是     http://bit.ly/jJa131 sdfdsfasd shankar (shankar)部分

如果你取出那个OUT(在JSON的两个部分中)并将它传递给jsonlint.com,json就会成功验证。

答案 2 :(得分:3)

我刚刚遇到了同样的错误,经过一些调试后,我注意到,其中一个字符串中有一个换行符引发了上述错误。我实际上在引号之前的字符串末尾有它。

{"One":"My String \n"}

答案 3 :(得分:1)

通常,这些错误可能由字符串文字引起,如:

  

\ r \ n

如果使用javascript的JSON.parse()函数,请确保正确编码此类文字