1

我一直在尝试将 json 和 ajax 与 jquery 一起使用,但遇到了一些麻烦。我正在尝试从 json 文件中获取数据以显示在我的页面上。

目前我只是想将它发送到控制台,但我在控制台中得到空值。我不确定我做对了什么,做错了什么,所以我只是想知道我是否可以得到一些指示。

这就是我的请求

$(document).ready(function() {
var json = (function () {
var json = null;
$.ajax({
    'async': false,
    'global': false,
    'url': 'js/refs.json',
    'dataType': "json",
    'success': function (refs) {
        json = refs;
    }
});
return json;
})(); 
console.log(json);

这就是 refs.json 中的内容

var refs = {
"referee": [
        {
            "name": "Ellie",
            "company": "University",
            "position": "Lecturer",
            "address": "",
            "phone": "5750",
            "email": "ellie@ac.uk",
            "type": "Education"
        },
        {
            "name": "Matthew",
            "company": "",
            "position": "",
            "address": "23 High Street",
            "phone": " 962",
            "email": "matthew@aaa.com",
            "type": "Character"
        }
    ],
"project": [
        {
            "tab": "Dissertation",
            "title": "Can technology in the home be used to enhance learning of numeracy, in conjunction with the nantional curriculum",
            "yr": "2013",
            "link": [
                {
                    "name": "Artefact",
                    "links": "fypc",
                    "size": "",
                    "misc": ""
                }
                ],
            "docs": [
                {"type": "doc",
                "links": "fyp.docx",
                "size" :"3.78MB",
                },
                {"type": "pdf",
                "links": "fyp.pdf",
                "size" :"1.76MB",
                }
                ],
            "purpose": "School - Anglia Ruskin University",
            "grade": "Not yet awarded",
            "sshot": "fypc.png"
        },
        {
            "tab": "Network and IT Operations",
            "title": "Virtual inter-office network with firewall. (Built using PacketTracer 5.3.3)",
            "yr": "2013",
            "link": [
                {
                    "name": "Serial Cable Connection Version",
                    "links": "",
                    "size": "204KB",
                    "misc": "(Submitted version)"
                },
                {
                    "name": "Frame Relay Version",
                    "links": "",
                    "size": "129KB",
                    "misc": ""
                },
                {
                    "name": "Packet Tracer 5.3.3 Download",
                    "links": "",
                    "size": "48.2MB",
                    "misc": "(.zip)"
                }
            ],
            "docs": [
                {
                    "type": "doc",
                    "links": "nio.docx",
                    "size" :"223KB",
                },
                {
                    "type": "pdf",
                    "links": "nio.pdf",
                    "size" :"943.KB",
                }
                ],
            "purpose": "School - Anglia Ruskin University",
            "grade": "Not yet awarded",
            "sshot": "nio1.png"
        }
    ]
};

正如我所说,控制台使用 console.log 的响应为空。我看不出我要去哪里对或错。该请求是我从此处的帖子中获得的片段(将json 加载到变量中)

任何指针将不胜感激

提前致谢

4

4 回答 4

4

您的文件不是 JSON!

它开始于var refs = ....

禁止赋值(和尾随分号)。

(如果您真的很懒,请从@MikeB 的答案中复制/粘贴文件中应该包含的内容)

于 2013-05-29T21:45:32.623 回答
2

我注意到的一件事是您的 JSON 无效。

39 号线"size": "3.78MB",

第 44 行"size": "1.76MB",

79 号线"size": "223KB",

都有一个额外的逗号

尝试将其用作您的 JSON

{
    "referee": [
        {
            "name": "Ellie",
            "company": "University",
            "position": "Lecturer",
            "address": "",
            "phone": "5750",
            "email": "ellie@ac.uk",
            "type": "Education"
        },
        {
            "name": "Matthew",
            "company": "",
            "position": "",
            "address": "23 High Street",
            "phone": " 962",
            "email": "matthew@aaa.com",
            "type": "Character"
        }
    ],
    "project": [
        {
            "tab": "Dissertation",
            "title": "Can technology in the home be used to enhance learning of numeracy, in conjunction with the nantional curriculum",
            "yr": "2013",
            "link": [
                {
                    "name": "Artefact",
                    "links": "fypc",
                    "size": "",
                    "misc": ""
                }
            ],
            "docs": [
                {
                    "type": "doc",
                    "links": "fyp.docx",
                    "size": "3.78MB"
                },
                {
                    "type": "pdf",
                    "links": "fyp.pdf",
                    "size": "1.76MB"
                }
            ],
            "purpose": "School - Anglia Ruskin University",
            "grade": "Not yet awarded",
            "sshot": "fypc.png"
        },
        {
            "tab": "Network and IT Operations",
            "title": "Virtual inter-office network with firewall. (Built using PacketTracer 5.3.3)",
            "yr": "2013",
            "link": [
                {
                    "name": "Serial Cable Connection Version",
                    "links": "",
                    "size": "204KB",
                    "misc": "(Submitted version)"
                },
                {
                    "name": "Frame Relay Version",
                    "links": "",
                    "size": "129KB",
                    "misc": ""
                },
                {
                    "name": "Packet Tracer 5.3.3 Download",
                    "links": "",
                    "size": "48.2MB",
                    "misc": "(.zip)"
                }
            ],
            "docs": [
                {
                    "type": "doc",
                    "links": "nio.docx",
                    "size": "223KB"
                },
                {
                    "type": "pdf",
                    "links": "nio.pdf",
                    "size": "943.KB"
                }
            ],
            "purpose": "School - Anglia Ruskin University",
            "grade": "Not yet awarded",
            "sshot": "nio1.png"
        }
    ]
}
于 2013-05-29T21:46:47.087 回答
1

您在分配数据之前返回 Json var。请测试此更改

var json= null;
$(document).ready(function() {

$.ajax({
    'async': false,
    'global': false,
    'url': 'js/refs.json',
    'dataType': "json",
    'success': function (refs) {
        json= refs;
        LoadedJSON();
    }
});
}); 

function LoadedJSON(){
console.log(json);
}
于 2013-05-29T21:50:05.633 回答
0

您可以使用 eval,一个本机 javascript 函数,将服务器响应(带有 json 格式的纯文本)转换为 json 对象。如果您使用某种 javascript 库,则那里必须有函数。在 jQuery 中有 Jquery。parseJson并且在 Dojo 中有fromJson

于 2013-05-29T22:15:31.943 回答