如何在PHP中回显来自JSON的特定key => value对?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (252)

我正在访问本地网络中的API。我需要为其他东西使用一些值,但我不知道如何从这个JSON数组中获取特定的键值对。

要获得JSON,我有:

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

这给了我:

 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] => ) 

我只需要:

temperature [value] => 62.61

但我不知道得到它的语法。

提问于
用户回答回答于
 $temperature = $json->data->{'1E5410ECC9D90FC3'}->entity->{'0'}->measurement->{0};

扫码关注云+社区

领取腾讯云代金券