I have a JSON array that looks like this (shortened for simplicity):
[
{
"name": null,
"code": null,
"results": [
{
"date": "2012-04-28T06:00:00.000Z",
"name": null,
"value": 135,
"unit": "MG/DL",
"code": null
},
{
"date": "2012-04-28T06:00:00.000Z",
"name": null,
"value": 59,
"unit": "MG/DL",
"code": null
}
]
},
etc, etc,
]
I need to convert this array into a JavaScript array so I can extract the date and value for plotting purposes. I've heard about using eval and JSON.parse, but I'm struggling to access the values correctly.
The JSON data is stored in a variable called labs, so I try the following:
var obj = JSON.parse(labs);
alert("obj.length="+obj.length); // correctly shows 22 objects
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
alert("prop: " +prop + " value: " +obj[prop]);
}
}
However, the output I get is: prop: 0 value:[object Object] prop: 1 value:[object Object] and so on.
Can someone help me understand how to access the date and value fields in this scenario?