[英]JSON Javascript escape
所以我在下面有一些示例動態JSON,我遇到的麻煩是正確地轉義了所有內容,以便可以通過JSON.parse或Jquery.parseJSON進行正確處理,由於某種原因,當前它不是。 我嘗試替換所有引號,但不能解決任何問題...
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"}
。
在JavaScript內部,字符串文字中的引號和反斜杠需要使用反斜杠進行轉義: "string with \\\\backslash and \\" quote"
。
如果確實需要在JS字符串文字中使用JSON(沒有理由這樣做),則需要對它們進行兩次轉義: json = "{\\"key":\\"prop with \\\\\\" quote and \\\\n linebreak\\"}"
。對於“ Windows Phone”周圍的引號,您尚未這樣做。
但是,在處理此類問題時,您一定做錯了。 通常,您可以從ajax調用等獲取JSON字符串,而在這些字符串中,它們已經作為字符串值獲取了。 如果您想將一些由服務器創建的JSON直接回顯到js腳本中,則無需將其包裝在字符串文字中-它已經[幾乎]有效的Object Literal語法。
您的問題可能是整個Json
對象只是一個字符串,因為開頭和結尾都有引號。 JSON的想法是將復雜變量分配給一個對象,如下所示:
var Json = {
"resolved_id": "244296544",
...
}
同樣,也無需逃脫正斜杠。
一直到最后:
'...\n\n"}';
轉義反斜杠:
'...\\n\\n"}';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.