Reputation: 53
So I got this JSON Array in a variable named jsonObject:
jsonObject = {
"log": [{
"date": "15/09/2016",
"time": "15:35:56",
"temp": "16.0",
"humidity": "95.0"
}, {
"date": "15/09/2016",
"time": "15:35:59",
"temp": "30.0",
"humidity": "61.0"
}, {
"date": "15/09/2016",
"time": "15:36:03",
"temp": "30.0",
"humidity": "60.0"
}]
}
My goal is to iterate over it and place the data in a table and to do this I want the JSON Array as a normal array in Javascript. I have found many code examples on this, but none of them take into account the name of the array in this case "log". Anyone know how I can get rid of the name and just get an array? I could make the JSON array itne a string, substring it and then convert it into a JSON Array again and then convert it to a Array, but it feels very ineffeficent. Perhaps there is a way to create a 2D array of the JSON Array as a string but I don't know how.
Upvotes: 0
Views: 4333
Reputation: 343
If you want to iterate over the array, try something like this:
var jsonObj = {
"log": [{
"date": "15/09/2016",
"time": "15:35:56",
"temp": "16.0",
"humidity": "95.0"
}, {
"date": "15/09/2016",
"time": "15:35:59",
"temp": "30.0",
"humidity": "61.0"
}, {
"date": "15/09/2016",
"time": "15:36:03",
"temp": "30.0",
"humidity": "60.0"
}]
};
var jsonArr = jsonObj['log'];
for(var i in jsonArr){
console.log(JSON.stringify(jsonArr[i]));
}
Upvotes: 0
Reputation: 3636
JSON objects are javascript variables. If you want log, just grab log.
jsonObject = {
"log": [{
"date": "15/09/2016",
"time": "15:35:56",
"temp": "16.0",
"humidity": "95.0"
}, {
"date": "15/09/2016",
"time": "15:35:59",
"temp": "30.0",
"humidity": "61.0"
}, {
"date": "15/09/2016",
"time": "15:36:03",
"temp": "30.0",
"humidity": "60.0"
}]
}
logArray = jsonObject.log
Upvotes: 1