5

How do I get data from a json file?

 3 years ago
source link: https://www.codesd.com/item/how-do-i-get-data-from-a-json-file.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How do I get data from a json file?

advertisements

This question already has an answer here:

  • How do I return the response from an asynchronous call? 23 answers

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.

This is what I have for the request

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

This is what is in refs.json

var refs = {
"referee": [
        {
            "name": "Ellie",
            "company": "University",
            "position": "Lecturer",
            "address": "",
            "phone": "5750",
            "email": "[email protected]",
            "type": "Education"
        },
        {
            "name": "Matthew",
            "company": "",
            "position": "",
            "address": "23 High Street",
            "phone": " 962",
            "email": "[email protected]",
            "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"
        }
    ]
};

As I say, the response from the console using console.log is null. I can't see where I am going right or wrong. The request is a snippet I got from a post on here (load json into variable)

Any pointers would be much appreciated

Thanks in advance


Your file is not JSON!

It starts with var refs = ....

Suprress the assignment (and trailing semicolon).

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK