0

我正在做一个网络开发课程,我正在做一个练习,它包含了我迄今为止基于我的工作的文件,但我刚刚注意到我必须使用一个 json 文件,我已经完成了基本的 HTML、CSS , JavaScript 和 JQuery 在过去的几周里,并将所有这些文件很好地集成到了我的网页中,但是有了这个 json,我不太确定该怎么做,与其他资源相比,网络上关于 json 的资源远没有那么多,所以我希望这里有人可以帮助我,我尝试将其包含在我的网页中

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

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

一只忙碌的猫

下面是 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
]

}

4

3 回答 3

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 对象,而不是字符串。

有关更多信息,请参阅$.getJSON 的文档。

于 2013-02-18T14:53:54.883 回答
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 以动态构建或编辑网页以显示数据(但这超出了问题的范围......)

于 2013-02-18T14:56:19.057 回答
0

旧版 Dreamweavers 在以下情况下会出错:

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

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

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

但持续版本修复它。我现在使用的是 Dreamweaver CC 2015,并且已经放置了你所有的代码。它现在没有错误!

于 2015-06-26T17:33:02.260 回答