我有像这样的json响应,
"meteo_inverter_analysis":{
"Id93922.1":{
"inverter_id":"Id93922.1", "total_electricity_generated":1567.7910000000002
},
"Id93922.2":{
"inverter_id":"Id93922.2", "total_electricity_generated":1468.4869999999999
},
"Id93922.3":{
"inverter_id":"Id93922.3","total_electricity_generated":498.7319999999999
},
"Id93922.4":{
"inverter_id":"Id93922.4","total_electricity_generated":461.8369999999999
}
}
从该响应我想从total_electricity_generated创建一个js数组。该数组将是: var array = [1567.7910000000002,1468.4869999999999,498.7319999999999,461.8369999999999]
任何人都可以帮我怎么做? TIA
答案 0 :(得分:1)
您可以获取meteo_inverter_analysis
的密钥,然后循环到该密钥以获取Id93922.*
的属性值
var data = {"meteo_inverter_analysis":{
"Id93922.1":{
"inverter_id":"Id93922.1",
"total_electricity_generated":1567.7910000000002
},
"Id93922.2":{
"inverter_id":"Id93922.2",
"total_electricity_generated":1468.4869999999999
},
"Id93922.3":{
"inverter_id":"Id93922.3",
"total_electricity_generated":498.7319999999999
},
"Id93922.4":{
"inverter_id":"Id93922.4",
"total_electricity_generated":461.8369999999999
}
}
};
var keys = Object.keys(data.meteo_inverter_analysis);
var arr = keys.map(key => data.meteo_inverter_analysis[key].total_electricity_generated
);
console.log(arr);

答案 1 :(得分:1)
我认为以下示例将对您有所帮助:
<script>
var car =new Array();
var myObj, i;
myObj = {
"name":"John",
"age":30,
"cars":[ "Ford", "BMW", "Fiat" ]
};
for (i = 0; i < myObj.cars.length; i++) {
car[i]= myObj.cars[i] + "<br>";
}
</script>