2012-11-08 39 views
0

我不知道爲什麼我的json對象返回undefined。在我的JSON工具中,我沒有發現任何語法錯誤,但我有一種感覺,在構建它的過程中一定有什麼問題。試圖學習.ajax() - 我的json不斷拋出錯誤:「undefined」 - 我在做什麼?

我想開始製作一款殭屍遊戲,但對於我來說,第一步是瞭解如何將我創建的數據傳遞到前端。這是我一直希望實現的類AJAX,但我的控制檯是給我的錯誤:「未定義」

$(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。最終我會寫的PHP從數據庫中提取這個,並轉換爲json的視圖消耗:

{ 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" 
} 
+0

感謝您的代碼。錯誤說的究竟是什麼?它在你的代碼中發生了什麼? – Madbreaks

+0

所以我使用FF和Chrome。 「errorThrown」的值是「未定義的」 - 基本上它認爲整個json對象是未定義的......我可以通過以下方式糾正這一點:'1。我括住了整個陣列。 2.我括住了每個「bulletinquest」數組的全部內容3.我用雙引號將id包裹起來。**現在我遇到的問題是,當我嘗試訪問嵌套的_items_數組時 - 控制檯認爲它是undefined!** – user1809836

回答

2

這是無效的JSON。你需要用方括號中的整個事情,使一個數組:

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

注意,要檢查JSON是有效的,有大量的在線工具,可以幫助(例如,JSONLint)。 (你會在那裏看到,有效的JSON在技術上要求把鑰匙(id等)用雙引號括起來。)

+1

嗚呼!它現在有效。我根據您的建議做了幾件事: 1.我括住了整個陣列。 2.我括住了每個「bulletinquest」數組的所有內容 3.我用雙引號將id包裹起來。 成功。謝謝dbaseman! – user1809836

+0

鍵是不允許在json中具有相同的值EVER?我不斷收到「data [index] .bulletinrequest.items未定義。」嗯... – user1809836

+0

@ user1809836你不能在同一級重複鍵... – McGarnagle

相關問題