如何从json文件中获取数据?

我一直在尝试使用json和jjery的json和我遇到麻烦。 我试图从json文件中获取数据以显示在我的页面上。

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

这就是我对请求的要求

$(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的控制台响应为null。 我看不出我的正确与错误。 请求是我从这里的post得到的一个片段( 加载json到变量 )

任何指针都将非常感激

提前致谢

你的文件不是JSON!

它以var refs = ....开头

超越作业(和分号)。

(如果你真的很懒,请复制/粘贴@ MikeB答案中的文件内容)

我注意到的一件事是你的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" } ] } 

在分配数据之前,您将返回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); } 

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