【问题标题】:Json Parsing Error : JSON.parse: bad control character in string literalJson 解析错误:JSON.parse:字符串文字中的控制字符错误
【发布时间】:2012-03-13 13:29:06
【问题描述】:

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

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

以下是我的 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"}]

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

【问题讨论】:

  • 格式化您的 JSON 并接受之前问题的答案。
  • @EvilP 我已经在 jsonlint.com 中验证了这个 json,它给出了有效的 json 消息。
  • @AtulPatel 那么你的 json 本身没有问题,考虑到一些操作已经完成,或者可能会改变你的 json ?
  • 你说你验证它,然后发布不验证的无效 json ......你期望什么解决方案??
  • 您的 json 字符串有效。检查将处理处理 json 结果的代码。你有没有编写任何代码来检索 json 字符串的值?

标签: jquery jquery-mobile


【解决方案1】:

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

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

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

编码你的 json 字符串

yourJsonText = HttpUtility.JavaScriptStringEncode(yourJsonText)

【讨论】:

  • 3 年后,这帮助了我。谢谢!
【解决方案2】:

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

如果您将其取出(在 JSON 的两个部分中)并将其传递给 jsonlint.com,则 json 将成功验证。

【讨论】:

  • 感谢您的回复。实际上,由于我已在剪贴板中复制,因此我将这部分的 json 字符串放在单行中。所以这不是这个问题的一部分。因为我已经在 jsonlint.com 上验证了这个 json。
  • 如果 jsonlint 说它是有效的,那么它可能是有效的 - 也许你的调用代码编码/解码不正确?
  • 我写了 var obj = jQuery.parseJSON(msg.d); 这一行来解析这个 json 字符串,它给出了一个错误。 msg.d 返回此 json 字符串。
【解决方案3】:

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

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

【讨论】:

    【解决方案4】:

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

    \r\n

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-03
      • 1970-01-01
      • 2013-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-27
      • 1970-01-01
      相关资源
      最近更新 更多