简体   繁体   English

JSON.parse JavaScript字符串

[英]JSON.parse javascript string

In javascript, I need to parse a string into what would be the equivalent of a javascript array such as: 在javascript中,我需要将字符串解析为与javascript数组等效的字符串,例如:

[["2016-12-21",101.58],["2016-12-22",209.56]]

The data I have, along with my attempt at parsing it and the error message looks like this: 我拥有的数据,以及我尝试对其进行分析和出现的错误消息如下所示:

str = "[["2016-11-17",771.22998],["2016-11-16",764.47998],["2016-11-15",758.48999],["2016-11-14",736.080017],["2016-11-11",754.02002],["2016-11-10",762.559998],["2016-11-09",785.309998],["2016-11-08",790.51001],["2016-11-07",782.52002],["2016-11-04",762.02002],["2016-11-03",762.130005],["2016-11-02",768.700012],["2016-11-01",783.609985],["2016-10-31",784.539978]]"

JSON.parse(str)

Uncaught SyntaxError: Unexpected token & in JSON at position 2(…)

I attempted to remove some of the special characters from the string (&, #, ;, etc.), which slightly changed the error message, but I was never able to get to my end result. 我试图从字符串(&,#、;等)中删除一些特殊字符,这会稍微改变错误消息,但是我始终无法获得最终结果。 I figured I would put it in stackoverflow starting from the beginning, because there is most likely a better start-to-finish solution than what I was attempting. 我想从一开始就将其放入stackoverflow,因为最有可能比我尝试的解决方案更好。

Naively, 天真地

JSON.parse(str.replace(/"/g,'"'));

will work, but you should figure out why your double quotes are getting turned into HTML entities. 可以,但是您应该弄清楚为什么双引号变成了HTML实体。

Description 描述

This will allow for the numerical ascii character of any character. 这将允许任何字符的数字ascii字符。 However, if you only have quotations to remove it isn't too much to run a replace. 但是,如果您只有要删除的报价单,那么执行替换并不过分。

 var str = "[["2016-11-17",771.22998],["2016-11-16",764.47998],["2016-11-15",758.48999],["2016-11-14",736.080017],["2016-11-11",754.02002],["2016-11-10",762.559998],["2016-11-09",785.309998],["2016-11-08",790.51001],["2016-11-07",782.52002],["2016-11-04",762.02002],["2016-11-03",762.130005],["2016-11-02",768.700012],["2016-11-01",783.609985],["2016-10-31",784.539978]]"; // regex to get the ascii number var r = /\\&#(\\d\\d);/gi; // replaces each numerical ascii character with their character representation str = str.replace(r, function (match, grp) { return String.fromCharCode(parseInt(grp)); } ); console.log(JSON.parse(str)); 

Try to decode your encoded string reusing the HTML parser in something more portable, a lot faster, reusable. 尝试以更可移植,更快,可重用的方式重用HTML解析器来解码编码的字符串。 regex patterns are specific to the current problem and each time you encounter e different character you'll have write and rewrite a new pattern. 正则表达式模式特定于当前问题,每次遇到不同的字符时,您都会编写并重写一个新的模式。 I use an encapsulated function method similar to this: 我使用类似于以下的封装函数方法:

function htmlParse( x ) {
     var c = document.createElement("div");
         c.innerHTML = x;
  return c.innerText;       
 };

Now let's decode your retrieved string... 现在,让我们对检索到的字符串进行解码...

var str = "[["2016-11-17",771.22998],["2016-11-16",764.47998],["2016-11-15",758.48999],["2016-11-14",736.080017],["2016-11-11",754.02002],["2016-11-10",762.559998],["2016-11-09",785.309998],["2016-11-08",790.51001],["2016-11-07",782.52002],["2016-11-04",762.02002],["2016-11-03",762.130005],["2016-11-02",768.700012],["2016-11-01",783.609985],["2016-10-31",784.539978]]";

|>> | >>

var decoded = htmlParse( str );
console.log( decoded );

and see if parsing with json succeeds 看看json解析是否成功

var arr = JSON.parse( decoded );
console.log( arr );

and here is the snippet... 这是代码段...

  var str = "[["2016-11-17",771.22998],["2016-11-16",764.47998],["2016-11-15",758.48999],["2016-11-14",736.080017],["2016-11-11",754.02002],["2016-11-10",762.559998],["2016-11-09",785.309998],["2016-11-08",790.51001],["2016-11-07",782.52002],["2016-11-04",762.02002],["2016-11-03",762.130005],["2016-11-02",768.700012],["2016-11-01",783.609985],["2016-10-31",784.539978]]"; var decoded = htmlParse(str); console.log(decoded); var arr = JSON.parse(decoded); console.log(arr); function htmlParse(x) { var c = document.createElement("div"); c.innerHTML = x; return c.innerText; }; 

If using the json package to write the data, set the ensure_ascii argument to false. 如果使用json包写入数据,请将ensure_ascii参数设置为false。

with open('output.json', 'w', encoding='utf-8') as f:
    json.dump(data, f, ensure_ascii=False)

For Python2.x, use the codecs library to set the encoding of your output file when you open it. 对于Python2.x,在打开输出文件时,使用codecs库设置输出文件的编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM