如何从一个JSON文件中的数据? [英] How to get data from a json file?

查看:164
本文介绍了如何从一个JSON文件中的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用JSON和Ajax使用jQuery,我在一些麻烦运行。我想从一个JSON文件中的数据,以显示在我的网页。

I have been trying to use json and ajax with jquery and I am running in to some trouble. I am trying to get data from a json file to display on my page.

目前,我只是想将其发送到控制台,但是我得到空的控制台。我不知道我在做什么对,什么我做错了,所以我只是想知道如果我能得到一些指引。

At the moment I am just trying to send it to the console but I am getting null in the console. I am not sure what I am doing right and what I am doing wrong so I was just wondering if I could get some pointers.

这是我的请求

$(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

This is what is in 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成可变

任何指针将是非常美联社preciated

Any pointers would be much appreciated

在此先感谢

推荐答案

您的文件不是JSON!

Your file is not JSON!

它开始与 VAR裁判= ....

Suprress分配(和尾随分号)。

Suprress the assignment (and trailing semicolon).

(如果你真懒,复制/粘贴什么应该是从@ MikeB的答复文件中)

(if you are really lazy, copy/paste what should be in the file from @MikeB's answer)

这篇关于如何从一个JSON文件中的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆