Reputation: 163
I have created web service for mobile web site using mobile jquery and it will return json string.
When i start to parse this json using jQuery.parseJSON method, for sometimes it is working fine but in some situation ,it gives an error like:
JSON.parse: bad control character in string literal
.
I already validate this json on jsonlint.com and it given a valid json message.
Follwoing is my json string.
[{"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"}]
Please give me a solution for this issue. Thanks in advance.
Upvotes: 1
Views: 27885
Reputation: 965
Usually, those error can caused by string literals like:
\r\n
Be sure you properly encode this kind of literals if you use javascript's JSON.parse() function
Upvotes: 1
Reputation: 434
you can replace \n
with \\n
for example
yourJsonText = yourJsonText.Replace("\n","\\n");
but if you use asp.net , it's better to encode your json string by
yourJsonText = HttpUtility.JavaScriptStringEncode(yourJsonText)
Upvotes: 12
Reputation: 2805
Well I just had the same error and after some debugging, I noticed that, there was a newline in one of the strings which was throwing the above error. I actually had it at the end of the string just before the quote.
{"One":"My String \n"}
Upvotes: 3
Reputation: 3723
It's the
http://bit.ly/jJa131 sdfdsfasd
shankar (shankar)
part(s)
If you take that OUT (in both parts of the JSON) and pass it to jsonlint.com, the json validates succesfully.
Upvotes: 4