我有一个像这样的json文件
{
"doctors": [{
"name": "...",
"Section": "...",
"Grade": "..."
},
{
"name": "...",
"Section": "...",
"Grade": "..."
}],
"nurses": [{
"name": "...",
"Grade": "..."
},
{
"name": "...",
"Grade": "..."
},
{
"name": "...",
"Grade": "..."
},
{
"name": "...",
"Grade": "..."
}]
}
在html文件的脚本标记内,我定义了一个按下按钮时调用的loadData函数。
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);
}
现在我只能在json文件中获取整个数据集。但我需要分别检索每位医生的护士姓名/部门/性别。我尝试输入xo.responseText('doctors'[0])
甚至是xo.responseText([0][0])
,但都没有效果。
修改
我也试过解析responseText:JSON.parse(xo.responseText)
。但是我收到了这个错误:JSON.parse: expected ',' or '}' after property value in object at line 18
json文件位于我的本地光盘上,我使用firefox(开发人员版)获取输出。
答案 0 :(得分:0)
尝试:
JSON.parse(xo.responseText)["医生"] [0]
您需要解析请求响应,然后您可以选择数据。