我想读取页面http://mattrb.com/txt.txt上的json数据
例如,假设我想要获取名称“Bulbasaur”。我有这样的代码:
<?php
$file = file_get_contents("http://mattrb.com/txt.txt");
$json = json_decode($file);
echo $json->1->name;
?> 这段代码会导致php不能加载。这是因为你不能使用数字吗?接下来,我尝试了一下:
<?php
$file = file_get_contents("http://mattrb.com/txt.txt");
$json = json_decode($file);
$num = 1;
echo $json->$num->name;
?>这允许php加载,但仍然不返回任何内容。我做错了什么?
发布于 2016-08-02 01:15:46
您的json不应包含换行符和其他无效字符。换句话说,你的json是无效的。json_decode对该文件不起作用。
$file = file_get_contents("http://mattrb.com/txt.txt");
var_dump(json_decode($file));
// NULL发布于 2016-08-02 01:17:30
您的json无效。请到http://jsonlint.com/查看。
你也可以像这样访问php中的"1“:echo $json->{1}->name;
发布于 2016-08-02 01:19:41
您的json文件无效,有一个逗号为135的问题项,请尝试将其删除,以便可以解析该文件。
"135": {, //the problem of your json data is here
"levels": [5, 15],
"probability": 4 "name": "Jolteon",
"attack": 65,
"defense": 60,
"type": "electric",
"moves": [
"tackle",
"thundershock",
"thunder"
],
"curve": 1.6
},https://stackoverflow.com/questions/38704427
复制相似问题