我在从以下json对象获取使用数据时遇到问题:
{"version":"0.3","data":{"uuid":"010f5190-83a4-11e4-98cd-d9350366ee4b","from":1423262668135,"to":1454790652371,"min":[1454790652371,27.11945025092],"max":[1454790652371,27.11945025092],"average":27.119,"consumption":237506,"rows":362,"tuples":[[1454790652371,27.119,41140]]}}
我想回应消费数据"237506"
,但不会得到任何结果。
<?php
$url = 'http://vz.xx.de/middleware.php/data/010f5190-83a4-11e4-98cd-d9350366ee4b.json?from=1+year+ago&group=day&tuples=1';
$string = file_get_contents($url, true);
$result = json_decode($string);
echo $result->consumption;
?>
如果我回复$result->version
它可以毫无问题地工作......
答案 0 :(得分:0)
刚刚整理了JSON并意识到消费是在数据密钥内。
{
"version": "0.3",
"data": {
...
"consumption": 237506,
...
}
}
这就是您无法访问它的原因。首先尝试$result->data
,然后从此对象/数组中获取数据