试图学习.ajax() - 我的json不断抛出错误:“未定义” - 我在做什么?

时间:2012-11-08 17:17:58

标签: jquery getjson

我不知道为什么我的json对象返回undefined。我在JSON工具中没有看到任何语法上的错误,但我觉得我的构造方式一定有问题。

我想开始制作僵尸游戏,但对我而言,第一步是了解如何将我创建的数据传递到前端。这是我一直希望为类实现ajax的地方,但是我的控制台发送了一个错误:“undefined”

$(document).ready(function(){
                $.ajax({
                    url: 'dummy_json.json',
                    data : { load : true},
                    type: 'GET',
                    dataType: 'json',
                    timeout: 1000,
                    error: function(jqXHR, textStatus, errorThrown){
                        console.log("HOLY SHIT ZOMBIES!");
                        console.log(textStatus + " is " + errorThrown);
                    },
                    success: function(data){
                        $("body").append(data); // really basic append, more to come.
                    }
                });
            });

以下是dummy_json.json文件的内容,其中包含我手写的僵尸任务数据。我想从头开始这样做,这样我就可以很好地学习json。最后,我将编写从db中提取此内容并将其转换为json以供视图使用的php:

    { id : 0, bulletinquest : {
    items : [{
        zombie0 : {
            "regionoforigin" : "JKL",
            "zombiename" : " - Black",
            "genomeid" : "4778931",
            "reward" : "100",
            "imageName" : "4778931.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie1 : {
            "regionoforigin" : "JKL",
            "zombiename" : " - White",
            "genomeid" : "4779436",
            "reward" : "100",
            "imageName" : "4779436.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie2 : {
            "regionoforigin" : "JKL",
            "zombiename" : " II - Gray",
            "genomeid" : "6900326",
            "reward" : "45",
            "imageName" : "6900326.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie3 : {
            "regionoforigin" : "JKL",
            "zombiename" : " II - White",
            "genomeid" : "7009803",
            "reward" : "45",
            "imageName" : "7009803.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie4 : {
            "regionoforigin" : "JKL",
            "zombiename" : " III - Red",
            "genomeid" : "5715837",
            "reward" : "100",
            "imageName" : "5715837.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie5 : {
            "regionoforigin" : "GHI",
            "zombiename" : " II - Gray",
            "genomeid" : "6941414",
            "reward" : "45",
            "imageName" : "6941414.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie6 : {
            "regionoforigin" : "GHI",
            "zombiename" : " II - White",
            "genomeid" : "6930804",
            "reward" : "45",
            "imageName" : "6930804.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie7 : {
            "regionoforigin" : "DEF",
            "zombiename" : " - Blue",
            "genomeid" : "5597904",
            "reward" : "200",
            "imageName" : "5597904.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie8 : {
            "regionoforigin" : "DEF",
            "zombiename" : " III (Slow Walker) - Blue",
            "genomeid" : "5715212",
            "reward" : "45",
            "imageName" : "5715212.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie9 : {
            "regionoforigin" : "DEF",
            "zombiename" : " III (Slow Walker) - White",
            "genomeid" : "5715328",
            "reward" : "45",
            "imageName" : "5715328.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie10 : {
            "regionoforigin" : "DEF",
            "zombiename" : " III (Fast Walker) - White",
            "genomeid" : "6209005",
            "reward" : "200",
            "imageName" : "6209005.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie11 : {
            "regionoforigin" : "DEF",
            "zombiename" : " III (Fast Walker) - Blue",
            "genomeid" : "6209286",
            "reward" : "200",
            "imageName" : "6209286.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie12 : {
            "regionoforigin" : "DEF",
            "zombiename" : " II - Gray",
            "genomeid" : "6958754",
            "reward" : "300",
            "imageName" : "6958754.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie13 : {
            "regionoforigin" : "DEF",
            "zombiename" : " II - White",
            "genomeid" : "6958772",
            "reward" : "300",
            "imageName" : "6958772.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie14 : {
            "regionoforigin" : "ABC",
            "zombiename" : " III - Amber Brown",
            "genomeid" : "6901339",
            "reward" : "45",
            "imageName" : "6901339.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie15 : {
            "regionoforigin" : "ABC",
            "zombiename" : " III - Black",
            "genomeid" : "6901302",
            "reward" : "45",
            "imageName" : "6901302.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie16 : {
            "regionoforigin" : "ABC",
            "zombiename" : " III (Slow Walker) - White",
            "genomeid" : "5715328",
            "reward" : "45",
            "imageName" : "5715328.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie17 : {
            "regionoforigin" : "ABC",
            "zombiename" : " III (Slow Walker) - White",
            "genomeid" : "5716681",
            "reward" : "45",
            "imageName" : "5716681.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie18 : {
            "regionoforigin" : "ABC",
            "zombiename" : " III (Slow Walker) - Blue",
            "genomeid" : "5717547",
            "reward" : "45",
            "imageName" : "5717547.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie19 : {
            "regionoforigin" : "ABC",
            "zombiename" : " III (Fast Walker) - White",
            "genomeid" : "5717574",
            "reward" : "200",
            "imageName" : "5717574.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie20 : {
            "regionoforigin" : "DEF",
            "zombiename" : " III (Fast Walker) - Blue",
            "genomeid" : "5717583",
            "reward" : "200",
            "imageName" : "5717583.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        }
    }],
    bulletinquestType : "iHaveNoIdea",
    bulletinquestAmmount : "500",
    bulletinquestName : "Please help save the animal shelter from being overrun!"
}, id : 1, bulletinquest : {
    name : "FROGGA FETT PROMO WILL EAT YOU",
    items : [{
        zombie0 : {
            "regionoforigin" : "JKL",
            "zombiename" : " - Black",
            "genomeid" : "4778931",
            "reward" : "100",
            "imageName" : "4778931.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie1 : {
            "regionoforigin" : "JKL",
            "zombiename" : " - White",
            "genomeid" : "4779436",
            "reward" : "100",
            "imageName" : "4779436.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie2 : {
            "regionoforigin" : "JKL",
            "zombiename" : " II - Gray",
            "genomeid" : "6900326",
            "reward" : "45",
            "imageName" : "6900326.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie3 : {
            "regionoforigin" : "JKL",
            "zombiename" : " II - White",
            "genomeid" : "7009803",
            "reward" : "45",
            "imageName" : "7009803.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie4 : {
            "regionoforigin" : "JKL",
            "zombiename" : " III - Red",
            "genomeid" : "5715837",
            "reward" : "100",
            "imageName" : "5715837.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie5 : {
            "regionoforigin" : "GHI",
            "zombiename" : " II - Gray",
            "genomeid" : "6941414",
            "reward" : "45",
            "imageName" : "6941414.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie6 : {
            "regionoforigin" : "GHI",
            "zombiename" : " II - White",
            "genomeid" : "6930804",
            "reward" : "45",
            "imageName" : "6930804.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie7 : {
            "regionoforigin" : "DEF",
            "zombiename" : " - Blue",
            "genomeid" : "5597904",
            "reward" : "200",
            "imageName" : "5597904.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie8 : {
            "regionoforigin" : "DEF",
            "zombiename" : " III (Slow Walker) - Blue",
            "genomeid" : "5715212",
            "reward" : "45",
            "imageName" : "5715212.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie9 : {
            "regionoforigin" : "DEF",
            "zombiename" : " III (Slow Walker) - White",
            "genomeid" : "5715328",
            "reward" : "45",
            "imageName" : "5715328.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie10 : {
            "regionoforigin" : "DEF",
            "zombiename" : " III (Fast Walker) - White",
            "genomeid" : "6209005",
            "reward" : "200",
            "imageName" : "6209005.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie11 : {
            "regionoforigin" : "DEF",
            "zombiename" : " III (Fast Walker) - Blue",
            "genomeid" : "6209286",
            "reward" : "200",
            "imageName" : "6209286.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie12 : {
            "regionoforigin" : "DEF",
            "zombiename" : " II - Gray",
            "genomeid" : "6958754",
            "reward" : "300",
            "imageName" : "6958754.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie13 : {
            "regionoforigin" : "DEF",
            "zombiename" : " II - White",
            "genomeid" : "6958772",
            "reward" : "300",
            "imageName" : "6958772.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie14 : {
            "regionoforigin" : "ABC",
            "zombiename" : " III - Amber Brown",
            "genomeid" : "6901339",
            "reward" : "45",
            "imageName" : "6901339.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie15 : {
            "regionoforigin" : "ABC",
            "zombiename" : " III - Black",
            "genomeid" : "6901302",
            "reward" : "45",
            "imageName" : "6901302.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie16 : {
            "regionoforigin" : "ABC",
            "zombiename" : " III (Slow Walker) - White",
            "genomeid" : "5715328",
            "reward" : "45",
            "imageName" : "5715328.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie17 : {
            "regionoforigin" : "ABC",
            "zombiename" : " III (Slow Walker) - White",
            "genomeid" : "5716681",
            "reward" : "45",
            "imageName" : "5716681.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie18 : {
            "regionoforigin" : "ABC",
            "zombiename" : " III (Slow Walker) - Blue",
            "genomeid" : "5717547",
            "reward" : "45",
            "imageName" : "5717547.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie19 : {
            "regionoforigin" : "ABC",
            "zombiename" : " III (Fast Walker) - White",
            "genomeid" : "5717574",
            "reward" : "200",
            "imageName" : "5717574.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        },
        zombie20 : {
            "regionoforigin" : "DEF",
            "zombiename" : " III (Fast Walker) - Blue",
            "genomeid" : "5717583",
            "reward" : "200",
            "imageName" : "5717583.png",
            "moreinfo" : "To kill a zombie you must destroy the brain."
        }
    }],
    bulletinquestType : "savior",
    bulletinquestAmmount : "Over 9000",
    bulletinquestName : "Clear the brooklyn tunnel"
}

1 个答案:

答案 0 :(得分:2)

这是无效的JSON。你需要将整个东西包在方括号中,使其成为一个数组:

[ { id : 0, ... }, 
  { id: 1, ... },
  ...
]

注意,要检查JSON是否有效,有很多可以提供帮助的在线工具(例如JSONLint)。 (你会看到那里有效的JSON技术上要求将密钥(id等)包装在双引号中。)