7

所以我在下面有一些我的示例动态 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"}';
4

4 回答 4

5

在 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 语法。

于 2012-11-01T15:46:21.940 回答
1

您的问题可能是您的整个Json对象只是一个字符串,因为开头和结尾的引号。JSON 的想法是将复杂变量分配给一个对象,如下所示:

var Json = {
  "resolved_id": "244296544",
  ...
}

此外,无需转义正斜杠。

于 2012-11-01T15:46:44.187 回答
0

一路走到最后:

'...\n\n"}';

转义反斜杠:

'...\\n\\n"}';
于 2012-11-01T15:42:03.050 回答
0

根据JSONLint,您的问题出在这一行:

"title": "Windows Phone 7 Connector for Mac updated for WP8, rebranded simply as \'Windows Phone\'",

如果您删除 ' 上的反斜杠,它会验证。在JSON中,您不会逃避 '

不幸的是,由于您使用 ' 来分隔字符串,因此您需要找到另一种方法来转义它。您可以使用 \u0027 代替 \'。

于 2012-11-01T15:47:32.073 回答