easports611 easports611 - 16 days ago 9
Javascript Question

JSON.parse javascript string

In javascript, I need to parse a string into what would be the equivalent of a javascript array such as:

[["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.

Answer

Naively,

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

will work, but you should figure out why your double quotes are getting turned into HTML entities.

Comments