-2

I'm accessing an API in my local network. I need to use some values for other things, but I do not know how to get a specific key=>value pair from this JSON array.

To get the JSON, I have:

$url = 'http://192.168.123.123/api/dev';
$result = file_get_contents($url);
$resultData = json_decode($result);
//echo "<pre>";
print_r($resultData);
echo "<br>";

Which gives me:

 stdClass Object ( [data] => stdClass Object ( [1E5410ECC9D90FC3] => stdClass Object ( [type] => BB-TH [state] => normal [alarm] => stdClass Object ( [state] => none [severity] => ) [name] => Watchdog 15 [label] => Watchdog 15 [entity] => stdClass Object ( [0] => stdClass Object ( [name] => Watchdog 15 [alarm] => stdClass Object ( [state] => none [severity] => ) [measurement] => stdClass Object ( [0] => stdClass Object ( [type] => temperature [value] => 62.61 [state] => normal [alarm] => stdClass Object ( [state] => clear [severity] => ) [units] => F [datalogEnabled] => 1 ) [1] => stdClass Object ( [type] => humidity [value] => 47 [state] => normal [alarm] => stdClass Object ( [state] => none [severity] => ) [datalogEnabled] => 1 ) [2] => stdClass Object ( [type] => dewpoint [value] => 42.13 [state] => normal [alarm] => stdClass Object ( [state] => none [severity] => ) [units] => F [datalogEnabled] => 1 ) ) ) ) [layout] => stdClass Object ( [0] => Array ( [0] => entity/0 ) ) [order] => 0 [temperatureOffset] => 0 [snmpInstance] => 1 ) ) [retCode] => 0 [retMsg] => ) 

All I need is:

temperature [value] => 62.61

but I don't know the syntax to get it.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
DevOpsSauce
  • 1,319
  • 1
  • 20
  • 52

1 Answers1

-3

https://ideone.com/GowSeA

 $temperature = $json->data->{'1E5410ECC9D90FC3'}->entity->{'0'}->measurement->{0};
Alex
  • 16,739
  • 1
  • 28
  • 51
  • The $str variable in that link is static, correct? This will be a dynamic value I need to access. – DevOpsSauce Jul 23 '18 at 20:09
  • I don't care where you will get that string from. That is just an example you've provided and the important part is just about how to access elements in json object using php. – Alex Jul 23 '18 at 20:10
  • I figured it out and posted it in my question. Thanks for your help. – DevOpsSauce Jul 23 '18 at 20:50
  • 1
    [This answer is being discussed on meta](https://meta.stackoverflow.com/questions/371470/what-is-the-point-of-flagging). – user202729 Jul 24 '18 at 04:57
  • 1
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. -- see [meta post](https://meta.stackoverflow.com/questions/300837/what-comment-should-i-add-to-code-only-answers). – user202729 Jul 24 '18 at 05:02
  • 1
    (1) (as the meta post mentioned, it depends on context. For some very-short code snippet it may not be necessary, although a link to docs of relevant functions can be more helpful. For long code it is definitely necessary.) (2) It's considered bad practice on [so] to answer bad questions (see the close reasons for possible reasons for a question being bad: unclear/debug question lacks MCVE/duplicate/...) If you can, please improve the question to be good and then answer. – user202729 Jul 24 '18 at 05:17