我这里有代码,可以使用curl获取json数据。现在我想回复所有数据。这将显示无输出。
<?php
$json = file_get_contents('myurl');
$data = json_decode($json);
print_r($data);
?>
这是json来自我的网址:
({"Response_Code":"0000","ResultMobilePrefix":["0917","0905","0906","0915","0916","0926","0927","0937","0935","0817","0936","0922","0923",
"0932","0933","0934","0942","0943","0907","0908","0909","0910","0912","0918","0919","0920","
0921","0928","0929","0930","0938","0939","0948","0949","0925","0989","0999","0947","0998","
0946","0975","0977"]});
答案 0 :(得分:0)
你需要的只是 echo json_encode($ json);
答案 1 :(得分:0)
使用true
将其转换为数组;
$data = json_decode($json,true);
echo '<pre>';
print_r($data);
答案 2 :(得分:0)
以格式使用json_encode()
显示json,并使用JSON_PRETTY_PRINT
header('Content-type: application/json');
echo json_encode($json,JSON_PRETTY_PRINT);
答案 3 :(得分:0)
在使用json_decode()之前,您需要首先删除JSON文件上的括号和分号;
<?php
$json = file_get_contents('myurl');
//remove the brackets
$json = str_replace("(", "", $json);
$json = str_replace(")", "", $json);
//remove the semicolon
$json = str_replace(";", "", $json);
$data = json_decode($json);
print_r($data);
?>
这有点难看,但希望你明白这个想法,你需要首先删除那个角色
答案 4 :(得分:0)
我只想添加Manish上面解释的内容。这就是PHP Docs关于第二个参数的说法:
When TRUE, returned objects will be converted into associative arrays.
否则你只需获得一个stdclass
对象
答案 5 :(得分:0)
问题是URL没有返回有效的Json
你可以试试
var_dump($data);
这将返回 null ,因为它不是有效的json,请参阅json_decode
这将起作用
$json = file_get_contents('myurl');
$json = preg_replace('/[ ]{2,}|[\t\n\r\(\)\;]/', '', trim($json));
$data = json_decode($json);
print_r($data);