问题描述
所以我下面有一些我的动态JSON示例,我正在做的就是正确地转义所有内容,以便JSON.parse或Jquery.parseJSON正确处理它,由于某种原因它当前不是。我已经尝试更换所有引号,但它没有解决任何问题...
So i have some of my example dynamic JSON below, what i'm having trouble doing is escaping everything properly so that it is properly processed by JSON.parse or Jquery.parseJSON, which for some reason it currently isnt. I've tried replacing all quotes but it doesn't solve anything...
var Json = '{"resolved_id":"244296544","resolvedUrl":"http:\/\/www.engadget.com\/2012\/11\/01\/windows-phone-for-mac\/","host":"engadget.com","title":"Windows Phone 7 Connector for Mac updated for WP8, rebranded simply as \'Windows Phone\'","datePublished":"2012-11-01 04:49:00","timePublished":1351763340,"responseCode":"200","excerpt":"For Mac users who prefer Microsoft as their mobile partner, Windows Phone 7 Connector has been the one bridging the divide so far. The sync app has just been updated to v3.0, gaining support for Windows Phone 8 and a concise new name -- \"Windows Phone\" -- to match its Windows 8 counterpart.","authors":{"5437327":{"author_id":"5437327","name":"Deepak Dhingra","url":"http:\/\/www.engadget.com\/editor\/deepak-dhingra"}},"images":{"1":{"item_id":"244296544","image_id":"1","src":"http:\/\/www.blogcdn.com\/www.engadget.com\/media\/2012\/11\/win-phone-for-mac-1351752168.jpg","width":"0","height":"0","credit":"","caption":""}},"videos":"","wordCount":116,"isArticle":1,"isVideo":0,"isIndex":0,"usedFallback":0,"article":"\n<a href=\"http:\/\/www.engadget.com\/2012\/11\/01\/windows-phone-for-mac\/\" nodeIndex=\"493\"><img src=\"http:\/\/www.blogcdn.com\/www.engadget.com\/media\/2012\/11\/win-phone-for-mac-1351752168.jpg\" \/><span class=\"ril_caption\"> <cite><\/cite><\/span><\/a>\n<p nodeIndex=\"91\" scoreAddedToParent=\"37\">For Mac users who prefer Microsoft as their mobile partner, <a href=\"http:\/\/www.engadget.com\/2011\/08\/31\/windows-phone-7-mango-will-play-nicer-with-macs-update-your-con\/\" nodeIndex=\"495\">Windows Phone 7 Connector<\/a> has been the one bridging the divide so far. The sync app has just been updated to v3.0, gaining support for <a href=\"http:\/\/www.engadget.com\/2012\/10\/29\/windows-phone-8-review\/\" nodeIndex=\"496\">Windows Phone 8<\/a> and a concise new name -- \"Windows Phone\" -- to match its <a href=\"http:\/\/www.engadget.com\/2012\/10\/29\/microsft-adds-windows-phone-app-to-windows-store\/\" nodeIndex=\"497\">Windows 8 counterpart<\/a>. The new app plays well with <a href=\"http:\/\/www.engadget.com\/tag\/RetinaMacbookPro\/\" nodeIndex=\"498\">Retina Macs<\/a> too, while other goodies in the changelog include drag-and-drop capability for transferring files in either direction, along with support for iPhoto 9.3.2 and Aperture 3.3.2. Incoming WP8 devices such as the <a href=\"http:\/\/www.engadget.com\/2012\/10\/29\/htc-8x-review-windows-phone-8s-compact-flagship\/\" nodeIndex=\"499\">HTC 8X<\/a> and the <a href=\"http:\/\/www.engadget.com\/2012\/10\/04\/nokia-lumia-920-for-atandt-hands-on-a-windows-phone-8-flagship-wi\/\" nodeIndex=\"500\">Lumia 920<\/a> will also get enhanced ringtone features and allow battery life to be monitored via the app. Persuaded? Then collect your goods at the source link below.<\/p>\n\n"}';
推荐答案
在JSON内部,字符串中的引号需要使用反斜杠进行转义: {key:prop with \quote}
。
Inside JSON, quotes within strings need to be escaped with a backslash: {"key": "prop with \" quote"}
.
在JavaScript中,字符串文字中的引号和反斜杠需要使用反斜杠进行转义:string with \\ \\\backslash和\quote
。
Inside JavaScript, quotes and backslashes within string literals need to be escaped with a backslash: "string with \\backslash and \" quote"
.
如果你真的需要在JS字符串文字中使用JSON(没有理由要做到这一点),你需要双重逃避它们: json ={\key:\prop with \\\quote and \\\\
。对于Windows Phone周围的报价,你还没有这样做。
linebreak \ }
If you really would need to use JSON in JS string literals (there is no reason to do that), you would need to double-escape them: json = "{\"key":\"prop with \\\" quote and \\n linebreak\"}"
. You haven't done so for the quotes around "Windows Phone".
但是,在处理这些问题时你一定做错了。通常你从ajax调用中获取JSON字符串等,你可以将它们作为字符串值获取。如果你想将一些服务器创建的JSON直接回显到js脚本中,你不需要将它包装在一个字符串文字中 - 它已经是有效的Object Literal语法。
However, you must've done something wrong when dealing with such problems. Usually you get JSON strings from ajax calls and such, where you get them already as a string value. If you want to echo some sever-created JSON directly into a js script, you don't need to wrap it in a string literal - it is already [nearly] valid Object Literal syntax.
这篇关于JSON Javascript逃脱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!