使用Javascript解析格式错误的JSON [英] Parsing malformed JSON with Javascript
问题描述
我想使用Java脚本解析此内容.数据如下:
I want to parse this content using Javascript. The data looks like this:
{"ss":[["Thu","7:00","Final",,"BAL","19","ATL","20",,,"56808",,"PRE4","2015"],["Thu","7:00","Final",,"NO","10","GB","38",,,"56809",,"PRE4","2015"]]}
每个在线教程都教您如何使用Twitter解析JSON,但是我不太确定如何使用JSON解析.
Every single tutorial online teaches you how to parse JSON using Twitter, but I am not quite sure how parsing with JSON works.
我想在网站上进行设置,以查看一个有趣的项目和有关解析JSON的良好学习体验的NFL团队分数,因为我不太在乎Twitter内容.
I would like to set this up on a website to view the NFL team scores for a fun project and a good learning experience about parsing JSON, as I could care less about Twitter stuff.
这可能吗?有什么好的入门教程吗?甚至一些起始代码?
Is this possible? Any good tutorials to start with? Even some starting code?
推荐答案
通常来说,您可以使用 JSON.解析来做到这一点.但是,您拥有的该片段似乎并非严格有效的JSON(如此处所示: http://jsfiddle.net/yK3Gf/,也可以通过在此处验证源JSON: http://jsonlint.com/).
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/).
因此,您将需要手工解析它,或者获取nfl.com来修复其JSON.
So you will either need to parse it by hand, or get nfl.com to fix up their JSON.
作为替代方案,使用eval()
时,其JSON可以成功解析,因此您可以使用以下内容进行解析:
As an alternative, their JSON does parse successfully when using eval()
, so you could parse it with something like:
var parsedData = eval('(' + jsonData + ')');
...如下所示: http://jsfiddle.net/yK3Gf/1/
尽管请注意,通常不赞成以这种方式解析JSON(尤其是当解析的数据由第三方来源传送时),因为如果数据碰巧包含任何可执行文件,它会使您容易受到XSS攻击里面的代码.
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.
这篇关于使用Javascript解析格式错误的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!