I'm not the greatest at JavaScript but I'm learning. I have a wireless sensor and a smart plug I'm practicing with. For some reason I cannot get to the property relay_state. I've tried Dot and Bracket but both return undefined. I'd like it as a function like what I have below but I"m not sure what I am doing wrong. Basically I just want the code to retrieve the value for relay_state from the returned parsed data. Appreciate the feedback! Thanks.
acState = JSON.parse(returnValue).result.responseData;
/*Returned response:
{
"system": {
"get_sysinfo": {
"sw_ver": "1.0.0",
"hw_ver": "1.0",
"type": "IOT.SMARTPLUGSWITCH",
"model": "MODELISHERE",
"mac": "MACIDISHERE",
"dev_name": "SMARTPLUG",
"alias": "PLUG ALIAS",
"relay_state": 0, //NEED THIS RETURNED
"on_time": 0,
"active_mode": "none",
"feature": "TIM",
"updating": 0,
"icon_hash": "",
"rssi": -53,
"led_off": 0,
"longitude_i": -LONGDATA,
"latitude_i": LATDATA,
"hwId": "HWIDISHERE",
"fwId": "00000000000000000000000000000000",
"deviceId": "DEVICEIDISHERE",
"oemId": "OEMIDISHERE",
"next_action": {
"type": -1
},
"err_code": 0
}
}
}
*/
function value(layer, path, value) {
var i = 0,
path = path.split('.');
for (; i < path.length; i++)
if (value != null && i + 1 === path.length)
layer[path[i]] = value;
layer = layer[path[i]];
return layer;
console.Log("Relay State: " + value(acState,'system[get_sysinfo[relay_state]]]'));
};
console.Log("Response Data Parsed: " + acState);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…