Ошибка синтаксического анализа Json: JSON.parse: неверный управляющий символ в строковом литерале - PullRequest
1 голос
/ 13 марта 2012

Я создал веб-сервис для мобильного веб-сайта, используя мобильный jquery, и он будет возвращать строку json.Когда я начинаю анализировать этот json с помощью метода jQuery.parseJSON, иногда он работает нормально, но в некоторых ситуациях выдает ошибку типа: JSON.parse: bad control character in string literal.

Я уже проверил этот json на jsonlint.com, и он выдал действительное сообщение 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 ]

12 голосов
/ 08 сентября 2012

вы можете заменить \n на \\n например

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

но если вы используете asp.net, лучше кодировать строку json с помощью

yourJsonText = HttpUtility.JavaScriptStringEncode(yourJsonText)

4 голосов
/ 13 марта 2012

Это
http://bit.ly/jJa131 sdfdsfasd shankar (shankar) часть (и)

Если вы возьмете это OUT (в обеих частях JSON) и передадите его jsonlint.com , json успешно выполнит проверку.

3 голосов
/ 10 июня 2012

Ну, у меня была та же самая ошибка, и после некоторой отладки я заметил, что в одной из строк была новая строка, которая выдавала вышеуказанную ошибку. У меня на самом деле было это в конце строки как раз перед кавычкой.

{"One":"My String \n"}
1 голос
/ 13 января 2013

Обычно эти ошибки могут быть вызваны строковыми литералами, такими как:

\ r \ n

Убедитесь, что вы правильно закодировали этот тип литералов, если используете функцию JSON.parse () javascript

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...