我试图从Json网址获取特定数据,但我却陷入了如何获取所需数据的困境。我的网址输出如下:
{
"dev_settings_device":{"uuid":"eneco-001-018946:hdrv_zwave_63523C6205B", "name":"settings_device", "internalAddress":"settings_device", "type":"settings_device", "supportsCrc":"49", "location":"(null)"},
"dev_10":{"uuid":"eneco-001-018946:hdrv_zwave_69423C65F08", "name":"HAE_METER_v2", "internalAddress":"10", "type":"HAE_METER_v2", "supportsCrc":"1", "supportedCC":"22 3c 3d 3e 56 60 70 72 7a 86 8b 73", "IsConnected":"1", "HealthValue":"10", "location":"(null)"},
"dev_10.1":{"uuid":"eneco-001-018946:hdrv_zwave_69448735F0E", "name":"HAE_METER_v2_1", "internalAddress":"10.1", "type":"gas", "supportsCrc":"0", "CurrentGasFlow":"0.00", "CurrentGasQuantity":"670.00", "location":"(null)"},
"dev_10.2":{"uuid":"eneco-001-018946:hdrv_zwave_6945CFF5F0E", "name":"HAE_METER_v2_2", "internalAddress":"10.2", "type":"elec", "supportsCrc":"0", "CurrentElectricityFlow":"428.00", "CurrentElectricityQuantity":"49464.00", "location":"(null)"},
"dev_10.3":{"uuid":"eneco-001-018946:hdrv_zwave_69458EC5F0E", "name":"HAE_METER_v2_3", "internalAddress":"10.3", "type":"elec_delivered_nt", "supportsCrc":"0", "CurrentElectricityFlow":"NaN", "CurrentElectricityQuantity":"NaN", "location":"(null)"},
"dev_10.4":{"uuid":"eneco-001-018946:hdrv_zwave_6947CCD5F0E", "name":"HAE_METER_v2_4", "internalAddress":"10.4", "type":"elec_received_nt", "supportsCrc":"0", "CurrentElectricityFlow":"NaN", "CurrentElectricityQuantity":"NaN", "location":"(null)"},
"dev_10.5":{"uuid":"eneco-001-018946:hdrv_zwave_694D7AB5F0E", "name":"HAE_METER_v2_5", "internalAddress":"10.5", "type":"elec_delivered_lt", "supportsCrc":"0", "CurrentElectricityFlow":"NaN", "CurrentElectricityQuantity":"NaN", "location":"(null)"},
"dev_10.6":{"uuid":"eneco-001-018946:hdrv_zwave_6941EFB5F0E", "name":"HAE_METER_v2_6", "internalAddress":"10.6", "type":"elec_received_lt", "supportsCrc":"0", "CurrentElectricityFlow":"NaN", "CurrentElectricityQuantity":"NaN", "location":"(null)"}
}
我的文件看起来像这样
<?php
error_reporting(E_ALL);
/* Written by Ierlandfan */
/* 1-11-2015 */
/* Version 1.1 */
/* Change ip_toon to the IP of your Toon */
/* start of current Energy usage import */
$file_string_gas = file_get_contents('http://192.168.1.66/hdrv_zwave?
action=getDevices.json');
$parsed_json = json_decode($file_string_gas, true);
var_dump($parsed_json['dev_10.1']);
/* Create new virtual sensor and write down the idx value */
/* Define idxvalue here */
$gasUsage=$parsed_json['CurrentGasQuantity'];
$idx = 933;
/* If this script is not run locally on the Domoticz server change 127.0.0.1
according to your Domoticz IP */
/* Send currentUsage to Domoticz */
$gasusage = curl_init("http://192.168.1.27:8080/json.htm?
type=command¶m=udevice&idx=$idx&nvalue=0&svalue=$gasUsage");
curl_exec($gasusage);
echo $gasUsage;
?>
我从var转储获得的输出是:
array(8) {
["uuid"]=>
string(39) "eneco-001-018946:hdrv_zwave_69448735F0E"
["name"]=>
string(14) "HAE_METER_v2_1"
["internalAddress"]=>
string(4) "10.1"
["type"]=>
string(3) "gas"
["supportsCrc"]=>
string(1) "0"
["CurrentGasFlow"]=>
string(4) "0.00"
["CurrentGasQuantity"]=>
string(6) "670.00"
["location"]=>
string(6) "(null)"
我收到的错误是:PHP注意:未定义的索引:第16行的/home/pi/domoticz/scripts/php/gas_data_json1.php中的CurrentGasQuantity,但对于我的生活,我不明白为什么。我尝试了几件事,但似乎我被卡住了。
答案 0 :(得分:1)
CurrentGasQuantity
是$parsed_json['dev_10.1']
的索引。所以你可以按如下方式检索它:
$gasUsage= $parsed_json['dev_10.1']['CurrentGasQuantity']
答案 1 :(得分:0)
轻微错误:
纠正,
$gasUsage= $parsed_json['dev_10.1']['CurrentGasQuantity']