I'm a ReactJS and axios newbie.
I want to iterate and extract json data if the key is number (like 0, 1, 2....) and don't know how can I write this piece of code. (because the server provide the json dynamically, and we don't know how many elements it will have)
Now I can extract the data with key = 0 (assume there exists this element)
class ContentBody extends Component {
constructor(props) {
super(props);
this.state = {
jdata: []
}
}
componentDidMount() {
var self = this;
// read data periodically
setInterval(function() {
axios.get(URL)
.then(function(response) {
self.setState({
jdata: response.data[0].Name
});
})
.catch(function(e) {
console.log("ERROR ", e);
})
}, 1000);
}
}
// below is json data from the server
{
"0": {
"Assigned": false,
"Name": "BebopDrone-Test001.",
"droneID": 0
},
"1": {
"Assigned": false,
"Name": "BebopDrone-B046836",
"droneID": 1
},
"2": {
"Assigned": false,
"Name": "BebopDrone-Test002.",
"droneID": 2
},
"function": "getAllDroneStatus()"
}
// my pseudo code might be
for(int i = 0; i < jsonObject.size(); i++){
if(key is number){
extract corresponding value
}
}