JSON基本错误

时间:2013-02-18 14:41:02

标签: javascript json web

我正在做一个web开发课,我正在做一个练习,它包含了我迄今为止基于我工作的文件,但我只是注意到了我必须使用的json文件,我已经完成了基础知识在过去的几周里,HTML,CSS,JavaScript和JQuery已经将所有这些文件整合到我的网页中了,但是对于这个json,我不太清楚该做什么,在网上关于json的数据远不及json所以我希望有人可以帮助我,我已经尝试将其包含在我的网页中

<script type='text/javascript' src='hall.json'></script>

我的主要问题是我无法在Dreamweaver中使用它,我在第2行遇到语法错误

a busy cat

以下是JSON代码

{
"who": "RSNO",
"what": "An American Festival",
"when": "2013-02-08 19:30",
"where": "User Hall - Main Auditorium",
"seats": [
    "00000000000000000011111111111111000000000000000000",
    "0000000000000001111111111111111aaa0000000000000000",
    "00000000000000aa111111111111111aaaaa00000000000000",
    "00000000000001111111111111111111111111000000000000",
    "000000000aa00aaaaaaaaaaaaaaaaaaaaaa1100aa000000000",
    "00000001111001111111111111111111111100111100000000",
    "00000aaaaaa0011aaaaaaaaa11111111aaa1100aaaaaa00000",
    "00001111111001111111111111111111111100111111100000",
    "000aaaaaaa110011111111111111111111110011aaaaaaa000",
    "00111111111100111111111111111111111001111111111000",
    "00aaaaa1111110011111111111111111111001111aaaaaaa00",
    "11111111111100111111111111111111111001111111111110",
    "0aaaaaaaaaaaa001111111111111111111100aaaaaaaaaaaa0",
    "01111111111110011111111111111111110011111111111100",
    "00000000000000001111111111111111110000000000000000",
    "01111111111111001111111111111111100111111111111100",
    "01111111111111001111111111111111110011111111111110",
    "01111111111111001111111111111111100111111111111100",
    "00a11111111111100111111111111111100111111111111a00",
    "00111111111111100111111111111111001111111111111000",
    "00011111111111110011111111111111001111111111111000",
    "00111111111111100111111111111111001111111111111000",
    "00011111111111110011111111111111001111111111111000",
    "00011111111111110011111111111110011111111111110000",
    "0000000111a111111001111a1111a110011111111110000000",
    "00000000111111110011111111111110011111111000000000",
    "00000000001111111001111111111110011111110000000000",
    "00000000000000111001111111111100111000000000000000"
],
"rows": [
    "DD",
    "CC",
    "BB",
    "AA",
    "Z",
    "Y",
    "X",
    "W",
    "V",
    "U",
    "T",
    "S",
    "R",
    "Q",
    "P",
    "N",
    "M",
    "L",
    "K",
    "J",
    "H",
    "G",
    "F",
    "E",
    "D",
    "C",
    "B",
    "A"
],
"seatPrice": [
    "                  00000000000000                  ",
    "               0000000000000000000                ",
    "              0000000000000000000000              ",
    "             0000000000000000000000000            ",
    "         00  000000000000000000000000  00         ",
    "       0000  00000000000000000000000  0000        ",
    "     000000  000000000000000000000000  000000     ",
    "    0000000  00000000000000000000000  0000000     ",
    "   000000000  0000000000000000000000  000000000   ",
    "  0000000000  000000000000000000000  0000000000   ",
    "  00000000000  00000000000000000000  00000000000  ",
    "000000000000  000000000000000000000  000000000000 ",
    " 000000000000  00000000000000000000  000000000000 ",
    " 000000000000  0000000000000000000  000000000000  ",
    "                000000000000000000                ",
    " 0000000000000  00000000000000000  0000000000000  ",
    " 0000000000000  000000000000000000  0000000000000 ",
    " 0000000000000  00000000000000000  0000000000000  ",
    "  0000000000000  0000000000000000  0000000000000  ",
    "  0000000000000  000000000000000  0000000000000   ",
    "   0000000000000  00000000000000  0000000000000   ",
    "  0000000000000  000000000000000  0000000000000   ",
    "   0000000000000  00000000000000  0000000000000   ",
    "   0000000000000  0011111111100  0000000000000    ",
    "       0000000000  111111111111  0000000000       ",
    "        00000000  1111111111111  00000000         ",
    "          0000000  111111111111  0000000          ",
    "              000  00000000000  000               "
],
"priceLookup": [
    10,
    20
]

}

3 个答案:

答案 0 :(得分:2)

您未在页面上正确包含JSON。 JSON不是像javascript那样被解释或编译的语言,它只是一种数据表示语言,如XML或CSV。要在您的页面中使用它,您可以为它发出ajax请求,然后在success回调中使用它:

$.getJSON('hall.json', function (data) {
    var who = data.who; // "RSNO"
    var where = data.where; // "User Hall - Main Auditorium"
    // etc...
});

请注意$.getJSON为您执行隐式JSON.parse,因此data已经是javascript对象,而不是字符串。

有关详细信息,请参阅documentation for $.getJSON

答案 1 :(得分:0)

您发布的JSON有效。

您收到语法错误,因为您尝试将其解释为JavaScript。

您可以编写一个读取JSON文件的脚本,并将内容解析为JavaScript对象。

一个非常基本的示例就是这样:

httpReq = new XMLHttpRequest();
httpReq.open('GET','hall.json',false);
httpReq.send();

hall = JSON.parse(httpReq.responseText);

然后,您可以使用JavaScript对象表示法访问内容:

who = hall.who; // 'RSNO'

seatsCount = hall.seats.length; // 28

操纵DOM以动态构建或编辑网页以显示数据(但这超出了问题的范围......)

答案 2 :(得分:0)

Old Dreamweavers版本在以下情况下出错:

{
"something":"content",
    "otherthing":"more content"
    }

所以你必须使用类似数组的东西:

[{"something":"content","otherthing":"more content"}]

但持续版本修复它。我现在正在使用Dreamweaver CC 2015,并且已经推出了所有代码。它现在没有错误!