【发布时间】:2017-02-28 17:51:28
【问题描述】:
我正在尝试从 PHP 中读取 JSON,如下所示:
[{
"titulo": "DontAsk",
"pais": "Austria",
"country_iso": "AT",
"direccion": "Mag. Th. Langmann Gmbh Landstrasse 4",
"cp_ciudad": "A-2000 STOCKERAU",
"lat": "48.385583",
"long": "16.207823",
"telefono": "43-2266-72554-11",
"fax": "43-2266-72554-44",
"web": "www.aaa.com"
}, {
"titulo": "Other One",
"pais": "Czech Republic",
"country_iso": "CZ",
"direccion": "Pod Cihelnou 6",
"cp_ciudad": "664 161 00 PRAHA 6",
"lat": "50.092605",
"long": "14.312707",
"telefono": "420 233 313 578",
"fax": "420 233 313 582",
"web": "www.bbb.com"
}]
JSON 没有错误,我尝试使用 JsonLint 并发现它很干净。 我有更多的插入,但我只插入了 2 个。
然后我尝试这样的代码行:
$json = json_decode(file_get_contents($url), true);
var_dump($json);
URL返回很好地检索URL,但是在var_dump中返回NULL
我看到了很多答案和问题,但没有找到答案。 有什么帮助吗?
读取 Json -> 在 PHP 中转换为数组 -> 检索数组
谢谢
【问题讨论】:
-
你有什么问题?
-
此 JSON 字符串已正确解析为
json_decode。 3v4l.org/3Qn0V 检查file_get_contents($url)是否给你预期的结果。 -
您基本上拥有一个执行多项任务并假设不会出错的单行程序。您需要进行错误检查。
标签: php arrays json decode file-get-contents