Home Ask Login Register

Developers Planet

Your answer is one click away!

alx February 2016

Get JSON object from URL

I am having problems in getting the consumption data from the following json object:

{"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]]}}

I would like to echo the consumption data "237506" but do not get any results.

<?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;

?>

If I echo $result->version it works without problems...

Answers


Dan Hanly February 2016

Just tidied the JSON and realised that consumption is within the data key.

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

This is why you can't access it. Try $result->data first then get your data from this object/array

Post Status

Asked in February 2016
Viewed 2,565 times
Voted 10
Answered 1 times

Search




Leave an answer


Quote of the day: live life