从 URL 获取 JSON 对象


Get JSON object from URL

我在从以下 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它可以毫无问题地工作......

刚刚整理了 JSON 并意识到消费在数据密钥内。

{
    "version": "0.3",
    "data": {
        ...
        "consumption": 237506,
        ...
    }
}

这就是您无法访问它的原因。先尝试$result->data然后从这个对象/数组中获取数据