I have a json file like this
{
"doctors": [{
"name": "...",
"Section": "...",
"Grade": "..."
},
{
"name": "...",
"Section": "...",
"Grade": "..."
}],
"nurses": [{
"name": "...",
"Grade": "..."
},
{
"name": "...",
"Grade": "..."
},
{
"name": "...",
"Grade": "..."
},
{
"name": "...",
"Grade": "..."
}]
}
Inside the script tag of the html file I have defined a loadData function that is called when a button is pressed.
function loadStaffData() {
var xo = new XMLHttpRequest();
xo.overrideMimeType("application/json");
xo.open('GET', 'staff_data.json', true);
xo.onreadystatechange = function () {
if (xo.readyState == 4 && xo.status == "200") {
var X=JSON.parse(xo.responseText);
console.log(X); //line 18
}
};
xo.send(null);
}
Right now I am only able to get the whole set of data in the json file. But I need to be ale to retrieve each doctor's/ nurse's name/ section/ gender separately. I tried typing xo.responseText('doctors'[0])
and even xo.responseText([0][0])
, but none of them worked.
Edit
I have also tried parsing the responseText: JSON.parse(xo.responseText)
. But I have got this error: JSON.parse: expected ',' or '}' after property value in object at line 18
The json file is on my local disc and I use firefox (developer edition) to get output.