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

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

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

Follwoing是我的json字符串。

[{"ID":"468438","Text":"http://bit.ly/jJa131 sdfdsfasd

shankar (shankar) on Ogoing Small Business Social Network

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://sofzh.miximages.com/jquery/music_arts_icon_large_634666567215996287.jpg"},{"ID":"468437","Text":"http://bit.ly/jJa131 asdfasdfasdfasdfasdf
shankar (shankar) on Ogoing Small Business Social Network
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://sofzh.miximages.com/jquery/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://sofzh.miximages.com/jquery/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://sofzh.miximages.com/jquery/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://sofzh.miximages.com/jquery/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://sofzh.miximages.com/jquery/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://sofzh.miximages.com/jquery/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://sofzh.miximages.com/jquery/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://sofzh.miximages.com/jquery/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://sofzh.miximages.com/jquery/img634661225851958174_org.png"}]

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

你可以用\\n替换\n例如

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

但是如果你使用asp.net,最好用你的json字符串进行编码

yourJsonText = HttpUtility.JavaScriptStringEncode(yourJsonText)

就是这样
http://bit.ly/jJa131 sdfdsfasd shankar (shankar) part’s

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

好吧,我只是有同样的错误,经过一些调试后,我注意到,其中一个字符串中有一个换行符抛出上述错误。 我实际上在引用之前的字符串末尾有它。

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

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

\ r \ n

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