Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从URL获取JSON对象_Php_Json_Decode_File Get Contents - Fatal编程技术网

Php 从URL获取JSON对象

Php 从URL获取JSON对象,php,json,decode,file-get-contents,Php,Json,Decode,File Get Contents,我在从以下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,"ro

我在从以下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
,然后从该对象/数组获取数据

刚刚整理了JSON,并意识到消费在数据键内

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

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

您是否尝试过$result->data->consumption?因为消耗是进入数据对象的,所以在$result=json\u decode($string)之后尝试var\u dump($result)尝试
echo$result->data->consumption有疑问时:谢谢,成功了!您是否尝试过$result->data->consumption?因为消耗是进入数据对象的,所以在$result=json\u decode($string)之后尝试var\u dump($result)尝试
echo$result->data->consumption有疑问时:谢谢,成功了!