json_decode 使用 PHP、NFL API 返回 null

发布于 2024-11-29 11:42:51 字数 466 浏览 3 评论 0原文

由于某种原因 json_decode 返回 NULL 和有效的 JSON。

json 位于此处: http://www.nfl.com/liveupdate/scorestrip/scorestrip .json

我正在获取页面的文件内容(file_get_contents),然后在其上运行 json_decode 。

还尝试了魔术引号的stripslashes b/c。

提前致谢, 菲尔

$json = file_get_contents('http://www.nfl.com/liveupdate/scorestrip/scorestrip.json');

var_dump(json_decode($json));

For some reason json_decode is returning NULL with valid JSON.

The json is located here: http://www.nfl.com/liveupdate/scorestrip/scorestrip.json

I am getting the file contents of the page (file_get_contents) and then running json_decode on it.

Also tried stripslashes b/c of magic quotes.

Thanks in advance,
Phil

$json = file_get_contents('http://www.nfl.com/liveupdate/scorestrip/scorestrip.json');

var_dump(json_decode($json));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

将军与妓 2024-12-06 11:42:51

空数组元素是无效的 JSON,即多个逗号之间没有值

["Thu","7:30","Final",,"BAL","6","PHI","13",,,"55424",,"PRE1","2011"]
                      ^ Here                ^ Here    ^Here

您可以尝试在多个逗号之间插入空字符串(感谢 hakre),即

$json = preg_replace('/,(?=,)/m', ',""', $json);
// I'm dubious about this working, don't have PHP to test but it works in JS

我不知道这是否会影响您继续使用数据的能力

The empty array elements are invalid JSON, ie the multiple commas with no values between

["Thu","7:30","Final",,"BAL","6","PHI","13",,,"55424",,"PRE1","2011"]
                      ^ Here                ^ Here    ^Here

You could try inserting empty strings between multiple commas (thanks hakre), ie

$json = preg_replace('/,(?=,)/m', ',""', $json);
// I'm dubious about this working, don't have PHP to test but it works in JS

I don't know if this messes up your ability to continue using the data though

冷心人i 2024-12-06 11:42:51

我得出了和 Phil 相同的结论,但我花了大约 5 分钟的时间。 :)

但是,我注意到他的输出有一个问题......它正在删除一个字段。

我不喜欢在正则表达式中进行这种模式检测,只是因为我认为它更难维护。在本例中,我只是通过 while 循环运行它。

while (strpos($json,',,'))
{
    $json = str_replace(',,',',"",', $json);
}

我确信菲尔可以想出一个合适的正则表达式,但这个解决方案将保持正确的条目数和条目数。他们的顺序位置。

I came the same conclusion as Phil, but it took me about 5 minutes longer. : )

However, I noticed a problem in his output... it is dropping a field.

I don't like doing this kind of pattern detection in RegEx, only because I think it is harder to maintain. In this case, I just run it through a while loop.

while (strpos($json,',,'))
{
    $json = str_replace(',,',',"",', $json);
}

I'm sure Phil can come up with a proper RegEx, but this solution will maintain the right # of entries & their ordinal positions.

断桥再见 2024-12-06 11:42:51

对于任何关注 2014 年的人。新的 feed 数据链接应该是:

http://www.nfl .com/liveupdate/scores/scores.json

For anyone looking in 2014. The new feed data link should be:

http://www.nfl.com/liveupdate/scores/scores.json

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文