2016-12-31 11 views
0

私はjavscriptのホームオートメーションシステムからJSON応答を解析しようとしています。 応答は、応答のほんの一部である available hereJSONレスポンスの値を解析して注文を変更する

あり、また、私は、SUは数インデックスを使用して知っていない理由のために再起動するたびにキーの変更の順​​序は文句を言わない、本当に私がする必要がある を働きますsensor.out、sensor.in、sensor.doorの状態値をandoridのtaskerの変数に格納できるようにする entity.idを使って選択しようとしましたが、何らかの理由でコードが終了しなかった

+1

あなたは、配列をループするために必要な、より速いノートで質問 –

+0

にあなたのコードを追加し、かどうかを確認'entity_id'です。一致する場合は、 'state'を記録します。 – 31piy

答えて

1

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);

0

インデックスを使用してオブジェクトからプロパティを選択しようとしている場合は、非常に具体的な理由がない限り、存在してはいけません。

幸い、それは問題ありません。注文を知る必要はありません。 JSON配列から2つのオブジェクトを取り出し、プロパティをスクランブルし、指定したkey/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> 
+0

特定のentity_idの状態値を取得するにはどうすればよいですか? –

+0

ええ、entity_idが含まれているobjから "state"の値を取得する場合は、次のようになります。sensor.backdoor? –

+0

thats right!あなたが指定したkey/valを含むオブジェクトを返すように関数を編集しました。 –

0

誰もが正しいと言っている:応答のキーの順序は関係ありません。数値インデックスではなく(文字列)キーを使用します。

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); 
関連する問題