Mas*_*345 13 php json file-get-contents
我有以下json
country_code({"latitude":"45.9390","longitude":"24.9811","zoom":6,"address":{"city":"-","country":"Romania","country_code":"RO","region":"-"}})
Run Code Online (Sandbox Code Playgroud)
我只想要country_code,我如何解析它?
我有这个代码
<?php
$json = "http://api.wipmania.com/jsonp?callback=jsonpCallback";
$jsonfile = file_get_contents($json);
var_dump(json_decode($jsonfile));
?>
Run Code Online (Sandbox Code Playgroud)
它返回NULL,为什么?
谢谢.
Sol*_*ili 27
<?php
$jsonurl = "http://api.wipmania.com/json";
$json = file_get_contents($jsonurl);
var_dump(json_decode($json));
?>
Run Code Online (Sandbox Code Playgroud)
你只需要json而不是jsonp.如果要返回数组,
也可以尝试使用 json_decode($json, true)
.
您正在请求 jsonp with http://api.wipmania.com/jsonp?callback=jsonpCallback
,它返回一个包含 JSON 的函数,例如:
jsonpCallback({"latitude":"44.9718","longitude":"-113.3405","zoom":3,"address":{"city":"-","country":"United States","country_code":"US","region":"-"}})
Run Code Online (Sandbox Code Playgroud)
而不是 JSON 本身。将您的 URL 更改为http://api.wipmania.com/json
返回纯 JSON,例如:
{"latitude":"44.9718","longitude":"-113.3405","zoom":3,"address":{"city":"-","country":"United States","country_code":"US","region":"-"}}
Run Code Online (Sandbox Code Playgroud)
请注意,第二块代码没有将 json 包装在函数中jsonpCallback()
。