0

我有这个字符串:

{\"description\": \"PSY - Gangnam Style (\\uac15\\ub0a8\\uc2a4\\ud0c0\\uc77c) \\n\\u25b6 NOW available on iTunes: http:\\\/\\\/Smarturl.it\\\/psygangnam\\n\\u25b6 Official PSY Online Store US \\u0026 International : http:\\\/\\\/psy.shop.bravadousa.com\\\/\\n\\u25b6 About PSY from YG Ent.: http:\\\/\\\/smarturl.it\\\/YGfamilyAboutPSY\\n\\u25b6 PSY's Products on eBay: http:\\\/\\\/stores.ebay.com\\\/ygentertainment\\n\\u25b6 YG-eShop: http:\\\/\\\/www.ygeshop.com\\n \\n===============================\\nPSY CONCERT \\\"HAPPENING\\\"\\n2013.4.13. SAT 6:30PM\\nTHE SEOUL WORLD CUP STADIUM\\nYouTube LIVE@ http:\\\/\\\/www.youtube.com\\\/officialpsy\\nTickets: http:\\\/\\\/smarturl.it\\\/PsyHappeningKor\\nEnglish Booking: http:\\\/\\\/smarturl.it\\\/PsyHappeningEng\\n===============================\\n\\nFor More Information @\\nhttp:\\\/\\\/www.facebook.com\\\/officialpsy\\nhttp:\\\/\\\/twitter.com\\\/psy_oppa\\nhttp:\\\/\\\/twitter.com\\\/ygent_official\\nhttp:\\\/\\\/me2day.net\\\/psyfive\\nhttp:\\\/\\\/www.psypark.com\\nApp Store: http:\\\/\\\/goo.gl\\\/l9TU6\\nGoogle Play: http:\\\/\\\/goo.gl\\\/UiEn1\\n\\n\\u00a9 YG Entertainment Inc. All rights reserved.\"}

我想用它来解析它,Json但它失败了。我尝试\""with 转换:

tmp = tmp.replace(/\\"/gi, '"'); 

并将其更改为:

{"description": "PSY - Gangnam Style (\\uac15\\ub0a8\\uc2a4\\ud0c0\\uc77c) \\n\\u25b6 NOW available on iTunes: http:\\\/\\\/Smarturl.it\\\/psygangnam\\n\\u25b6 Official PSY Online Store US \\u0026 International : http:\\\/\\\/psy.shop.bravadousa.com\\\/\\n\\u25b6 About PSY from YG Ent.: http:\\\/\\\/smarturl.it\\\/YGfamilyAboutPSY\\n\\u25b6 PSY's Products on eBay: http:\\\/\\\/stores.ebay.com\\\/ygentertainment\\n\\u25b6 YG-eShop: http:\\\/\\\/www.ygeshop.com\\n \\n===============================\\nPSY CONCERT \\"HAPPENING\\"\\n2013.4.13. SAT 6:30PM\\nTHE SEOUL WORLD CUP STADIUM\\nYouTube LIVE@ http:\\\/\\\/www.youtube.com\\\/officialpsy\\nTickets: http:\\\/\\\/smarturl.it\\\/PsyHappeningKor\\nEnglish Booking: http:\\\/\\\/smarturl.it\\\/PsyHappeningEng\\n===============================\\n\\nFor More Information @\\nhttp:\\\/\\\/www.facebook.com\\\/officialpsy\\nhttp:\\\/\\\/twitter.com\\\/psy_oppa\\nhttp:\\\/\\\/twitter.com\\\/ygent_official\\nhttp:\\\/\\\/me2day.net\\\/psyfive\\nhttp:\\\/\\\/www.psypark.com\\nApp Store: http:\\\/\\\/goo.gl\\\/l9TU6\\nGoogle Play: http:\\\/\\\/goo.gl\\\/UiEn1\\n\\n\\u00a9 YG Entertainment Inc. All rights reserved."}

但在这里我得到:

PSY CONCERT \\"HAPPENING\\"\\n2013.4.13. SAT 6:30PM

在其中一行中,我想知道是否有任何解码方法可以将字符串解码为有效解析为 json 对象?

4

3 回答 3

1

您的字符串似乎是双重转义的,但缺少外部分隔符。

var str = '{\\"description\\": \\"PSY - Gangnam Style (\\\\uac15\\\\ub0a8\\\\uc2a4\\\\ud0c0\\\\uc77c) … .\\"}';
var jsonStr = JSON.parse('"'+str+'"'),
    obj = JSON.parse(jsonStr);
于 2013-07-24T12:33:16.337 回答
0

它可能不是最好的解决方案,但是在将字符串解析为 JSON 之前评估它应该可以工作:

eval("tmp = \"" + tmp + "\";");
var tmpJSON = JSON.parse(tmp);

但请记住,您正在评估一个可能未知的字符串,这并不安全。也许那里有一个解码功能,它也是如此。

那么为什么要这样做呢?

你的字符串看起来像一个字符串,当我写一段代码时我会写它。它具有打印引号或反斜杠本身所需的所有转义反斜杠。但是那些转义字符不应该真正在字符串中,它们只是用来定义字符串应该是什么样子。所以,写作var str = "He said \"ok\"";会产生一个像He said "ok". 但是由于您的字符串实际上包含转义字符,因此将改为这样定义:

var str = "He said \\\"ok\\\"";

这将产生一个这样的字符串:

他说“好”

现在里面的一切都eval被解释为一段代码。所以当我写:

eval("str = \"" + str + "\";");

被执行的代码行是:

str = "He said \"ok\"";

这会产生所需的未转义字符串:

他说“好”

于 2013-07-24T11:54:07.060 回答
0

不如 basilikum 的方法优雅,也可能不如 Bergi 和 Jonathan Lonowsky 建议的双解析方法正确,但您可以使用Javascript 否定后向等价物,如下所示:

str.replace(/\\+"/gi, function(s){return (s.length % 2) ? s : s.slice(0,-2) + '"';})

这绝对比评估字符串更安全。

于 2013-07-24T11:56:04.850 回答