2017-07-09 113 views
0

我想從Json網址獲取特定數據,但我被困在如何獲得我需要的數據。我的網址的輸出看起來像:通過url Json信息php

{ 
"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&param=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注意:未定義的索引:CurrentGasQuantity在/home/pi/domoticz/scripts/php/gas_data_json1.php在第16行,但對於我的生活我不明白爲什麼。我嘗試了一些事情,但似乎我卡住了。

回答

0

小的失誤:

校正, $gasUsage= $parsed_json['dev_10.1']['CurrentGasQuantity']

+0

感謝就像一個魅力。非常感激 ! –

1

CurrentGasQuantity$parsed_json['dev_10.1']的索引。所以,你可以按照以下檢索:

$gasUsage= $parsed_json['dev_10.1']['CurrentGasQuantity']