无法使用json数据

时间:2017-03-06 17:01:30

标签: javascript json

我正在使用PHP脚本生成JSON字符串,我想在javascript中使用它来显示一些数据。问题是即使在使用json.parse之后我也无法访问json字符串数据。请帮帮我。

JSON字符串是:

{
    "8": {
        "dept_name": null,
        "dept_code": null,
        "int_jour": null,
        "nat_jour": null,
        "inter_nat_conf": null,
        "nat_conf": null,
        "int_book_chap": null,
        "nat_book_chap": null
    },
    "0": {
        "int_jour": 10,
        "nat_jour": 1,
        "inter_nat_conf": 16,
        "nat_conf": 14,
        "int_book_chap": 1,
        "nat_book_chap": 4,
        "dept_code": "101",
        "dept_name": "ECE"
    },
    "1": {
        "int_jour": 22,
        "nat_jour": 1,
        "inter_nat_conf": 32,
        "nat_conf": 16,
        "int_book_chap": 5,
        "nat_book_chap": 0,
        "dept_code": "102",
        "dept_name": "CSE"
    },
    "2": {
        "int_jour": 12,
        "nat_jour": 4,
        "inter_nat_conf": 10,
        "nat_conf": 23,
        "int_book_chap": 1,
        "nat_book_chap": 0,
        "dept_code": "103",
        "dept_name": "IT"
    },
    "3": {
        "int_jour": 21,
        "nat_jour": 0,
        "inter_nat_conf": 9,
        "nat_conf": 35,
        "int_book_chap": 0,
        "nat_book_chap": 0,
        "dept_code": "104",
        "dept_name": "EE"
    },
    "4": {
        "int_jour": 13,
        "nat_jour": 1,
        "inter_nat_conf": 8,
        "nat_conf": 33,
        "int_book_chap": 0,
        "nat_book_chap": 1,
        "dept_code": "105",
        "dept_name": "MCA"
    },
    "5": {
        "int_jour": 10,
        "nat_jour": 5,
        "inter_nat_conf": 12,
        "nat_conf": 13,
        "int_book_chap": 0,
        "nat_book_chap": 1,
        "dept_code": "106",
        "dept_name": "MBA"
    },
    "6": {
        "int_jour": 57,
        "nat_jour": 6,
        "inter_nat_conf": 5,
        "nat_conf": 10,
        "int_book_chap": 0,
        "nat_book_chap": 1,
        "dept_code": "109",
        "dept_name": "AS"
    },
    "7": {
        "int_jour": 0,
        "nat_jour": 0,
        "inter_nat_conf": 0,
        "nat_conf": 0,
        "int_book_chap": 0,
        "nat_book_chap": 0,
        "dept_code": "110",
        "dept_name": "CIVIL"
    }
}

javascript是:

<script>

var xmlhttp = new XMLHttpRequest();

xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        myObj = JSON.parse(this.responseText);
        document.getElementById("demo").innerHTML = myObj.[1].dept_name;

    }
};
xmlhttp.open("GET", "sss.php", true);
xmlhttp.send();

</script>

1 个答案:

答案 0 :(得分:5)

打开浏览器的开发者工具。看看控制台。阅读错误消息。

  

未捕获的SyntaxError:意外的令牌[

你有:

document.getElementById("demo").innerHTML = myObj.[1].dept_name;

您可以使用点符号方括号表示法来访问属性。你不能同时使用它们。

删除.之前的[