Parsing malformed JSON with Javascript

后端 未结 7 1629
抹茶落季
抹茶落季 2020-12-21 11:34

I want to parse this content using Javascript. The data looks like this:

{\"ss\":[[\"Thu\",\"7:00\",\"Final\",,\"BAL\",\"19\",\"ATL\",\"20\",,,\"56808\",,\"PRE

7条回答
  •  有刺的猬
    2020-12-21 11:56

    Generally speaking, you can use JSON.parse to do this. However, that snippet that you have does not appear to be strictly valid JSON (as seen here: http://jsfiddle.net/yK3Gf/ and also by validating the source JSON here: http://jsonlint.com/).

    So you will either need to parse it by hand, or get nfl.com to fix up their JSON.

    As an alternative, their JSON does parse successfully when using eval(), so you could parse it with something like:

    var parsedData = eval('(' + jsonData + ')');
    

    ...as shown here: http://jsfiddle.net/yK3Gf/1/

    Though be aware that parsing JSON in this way is generally frowned upon (particularly when the data being parsed is being delivered by a third-party source), as it leaves you open to XSS attacks should the data happen to include any executable code inside of it.

提交回复
热议问题