我試圖從javscript中的家庭自動化系統解析JSON響應。 響應是 available here從JSON響應中解析一個始終更改順序的值
那只是響應的一小部分,另外,在每次系統啓動鍵變化的原因,我不知道,蘇使用了數字索引的順序不會真的管用 我需要能夠將sensor.out,sensor.in,sensor.door的狀態值存儲到tasker on和oridid的變量中 我試圖使用entity.id進行選擇,但由於某種原因代碼從未完成(我相信我只是沒有知道我在做什麼)
我試圖從javscript中的家庭自動化系統解析JSON響應。 響應是 available here從JSON響應中解析一個始終更改順序的值
那只是響應的一小部分,另外,在每次系統啓動鍵變化的原因,我不知道,蘇使用了數字索引的順序不會真的管用 我需要能夠將sensor.out,sensor.in,sensor.door的狀態值存儲到tasker on和oridid的變量中 我試圖使用entity.id進行選擇,但由於某種原因代碼從未完成(我相信我只是沒有知道我在做什麼)
隨着ES6可以使用Array#find
方法:
response.find(o => o.entity_id == 'sensor.out').state
見片段:
var response = [ { "attributes":{ "friendly_name":"door" }, "entity_id":"sensor.door", "last_changed":"2016-12-31T11:15:59.395808+00:00", "last_updated":"2016-12-31T11:15:59.395808+00:00", "state":"closed" }, { "attributes":{ "friendly_name":"In", "unit_of_measurement":"\u00b0C" }, "entity_id":"sensor.in", "last_changed":"2016-12-31T11:20:02.179821+00:00", "last_updated":"2016-12-31T11:20:02.179821+00:00", "state":"20.7" }, { "attributes":{ "changed_by":null, "code_format":".+", "friendly_name":"panel" }, "entity_id":"alarm_control_panel.panel", "last_changed":"2016-12-31T11:14:56.471966+00:00", "last_updated":"2016-12-31T11:14:56.471966+00:00", "state":"disarmed" }, { "attributes":{ "friendly_name":"Out", "unit_of_measurement":"\u00b0C" }, "entity_id":"sensor.out", "last_changed":"2016-12-31T11:14:58.452345+00:00", "last_updated":"2016-12-31T11:14:58.452345+00:00", "state":"7.1" }];
var state = response.find(o => o.entity_id == 'sensor.out').state;
console.log('sensor.out state is', state);
或者,你可以在響應轉換爲對象用實體ID值作爲鍵,所以你可以一個CCESS它像response['session.out'].state
:
response = Object.assign({}, ...response.map(o => ({[o.entity_id]: o})));
見片段:
var response = [ { "attributes":{ "friendly_name":"door" }, "entity_id":"sensor.door", "last_changed":"2016-12-31T11:15:59.395808+00:00", "last_updated":"2016-12-31T11:15:59.395808+00:00", "state":"closed" }, { "attributes":{ "friendly_name":"In", "unit_of_measurement":"\u00b0C" }, "entity_id":"sensor.in", "last_changed":"2016-12-31T11:20:02.179821+00:00", "last_updated":"2016-12-31T11:20:02.179821+00:00", "state":"20.7" }, { "attributes":{ "changed_by":null, "code_format":".+", "friendly_name":"panel" }, "entity_id":"alarm_control_panel.panel", "last_changed":"2016-12-31T11:14:56.471966+00:00", "last_updated":"2016-12-31T11:14:56.471966+00:00", "state":"disarmed" }, { "attributes":{ "friendly_name":"Out", "unit_of_measurement":"\u00b0C" }, "entity_id":"sensor.out", "last_changed":"2016-12-31T11:14:58.452345+00:00", "last_updated":"2016-12-31T11:14:58.452345+00:00", "state":"7.1" }];
response = Object.assign({}, ...response.map(o => ({[o.entity_id]: o})));
console.log('sensor.out state is', response['sensor.out'].state);
如果你想使用索引來選擇對象的屬性,你不應該,除非有非常特殊的理由這樣做。
幸運的是,這很好,你不需要知道順序。我從你的JSON數組中取出了兩個對象,對這些屬性進行了編碼,然後編寫了一個函數,返回包含您指定的鍵/ val的任何對象。
你的問題有點難以遵循,但我認爲這會給你的想法。
<script type="text/javascript">
let arr = [
{
"attributes":{
"friendly_name":"door"
},
"entity_id":"sensor.frontdoor",
"last_changed":"2016-12-31T11:15:59.395808+00:00",
"last_updated":"2016-12-31T11:15:59.395808+00:00",
"state":"closed"
},
{
"last_changed":"2016-12-31T11:15:59.395808+00:00",
"state":"closed",
"attributes":{
"friendly_name":"door"
},
"entity_id":"sensor.backdoor",
"last_updated":"2016-12-31T11:15:59.395808+00:00"
}
];
function findKey (theKey, theVal) {
let reduced = arr.filter (d => {
return d [ theKey ] === theVal;
});
return reduced;
}
let targets = findKey ('entity_id', 'sensor.backdoor');
targets.forEach (d => {
// This check is a little naive, but should give you the idea
if ('state' in d) {
console.log (d.state);
}
});
</script>
我應該如何使用它來獲取特定entity_id的狀態值? –
嗯,你的意思是,如果你想從任何obj包含entity_id:sensor.backdoor的「狀態」值? –
這就是對的!對不起,沒有說明適當的 –
大家說的是正確的:響應中按鍵的順序無關緊要。使用(字符串)鍵,而不是數字索引。
var arr = [
{
"entity_id":"sensor.door",
"state":"closed"
}]; // other attributes chopped out for brevity
var entity_id_interested_in = 'sensor.door';
var state = '[entity_id not found in response]';
for (var i = 0; i < arr.length; i++) {
console.log(arr[i].entity_id + ' state:' + arr[i].state);
if (arr[i].entity_id == entity_id_interested_in)
{
state = arr[i].state;
break;
}
}
console.log (state);
你的代碼添加到這個問題 –
在更快的筆記,你通過數組需要循環,並檢查'entity_id'。如果匹配,則記錄「狀態」。 – 31piy