JavaScript JSon字符串解码

时间:2013-07-24 11:18:17

标签: javascript json

我有这个字符串:

{\"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解析它并且它失败了。我尝试将\""转换为:

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对象有效?

3 个答案:

答案 0 :(得分:1)

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

var str = '{\\"description\\": \\"PSY - Gangnam Style (\\\\uac15\\\\ub0a8\\\\uc2a4\\\\ud0c0\\\\uc77c) … .\\"}';
var jsonStr = JSON.parse('"'+str+'"'),
    obj = JSON.parse(jsonStr);

答案 1 :(得分: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\"";

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

  

他说“好”

答案 2 :(得分:0)

不像bowikum的方法那么优雅,也可能不如Bergi和Jonathan Lonowsky所建议的双解析方法那么正确,但你可以使用Javascript negative lookbehind equivalent,沿着这些方向:

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

这肯定比评估字符串更安全。