我正在使用文件获取内容方法来获取统计信息数据,如下所示
$val = file_get_contents('https://example.com/getStatsData');
我得到的输出值如下
{"msg":"success","data":[[{"stats_abc":"1223","stats_bcd":"55684","stats_cda":"14999","stats_def":232456,"stats_efg":"432868","stats_fgh":"7558","stats_ghi":"3,778.72","stats_date":"20-01-2018 17:00 PM"}],null]}
但是当我将其解码为json_decode($val(时。它没有显示任何东西。谁能帮我获取以上数据作为数组/对象?
提前致谢
这确实有效:
<?php
$json = '{
"msg": "success",
"data": [
[{
"stats_abc": "1223",
"stats_bcd": "55684",
"stats_cda": "14999",
"stats_def": 232456,
"stats_efg": "432868",
"stats_fgh": "7558",
"stats_ghi": "3,778.72",
"stats_date": "20-01-2018 17:00 PM"
}], null
]
}';
echo '<pre>';
print_r(json_decode($json));
echo '</pre>';
并导致:
stdClass Object
(
[msg] => success
[data] => Array
(
[0] => Array
(
[0] => stdClass Object
(
[stats_abc] => 1223
[stats_bcd] => 55684
[stats_cda] => 14999
[stats_def] => 232456
[stats_efg] => 432868
[stats_fgh] => 7558
[stats_ghi] => 3,778.72
[stats_date] => 20-01-2018 17:00 PM
)
)
[1] =>
)
)
只是为了表明 JSON 本身很好。
请尝试以下操作: 我已经把你的响应放在本地 JSON 文件和调用中,它工作正常。检查这个 :
<?php
$val= file_get_contents('http://localhost/j/test.json');
print_r(json_decode($val,true));
?>
首先确保$val不为空。然后使用 json_decode($val, 1)
.这将按预期将其转换为关联数组。