2012-02-12 40 views
2

我需要使用JQuery訪問一些JSON。出於某種原因,它無法正常工作,並且由於getJSON方法無法正常工作,我無法弄清楚什麼是錯誤的。我檢查了JSON網址是否正確。謝謝!使用JQuery訪問JSON

HTML:

<html> 
<head> 
    <script src="//code.jquery.com/jquery-1.6.4.min.js"></script> 
<script type="text/javascript"> 
     $(document).ready(function(){ 
     $.getJSON('../src/json/baseball.json', function(data) { 
      $("#add").html(data.baseball[0].levels[0].games[0].versus); 
     }); 
     }); 
    </script> 
</head> 
<body> 
    <p id= "add"></p> 
</body> 
</html> 

JSON:

{"baseball": 
[{ 
    "gender":"boys", 
    "levels":[ 
    { 
     "level": "varsity", 
     "games":[ 
     { 
      "versus":"Fullerton", 
      "homeaway":"Home", 
      "month":"February", 
      "date":"27", 
      "year":"2012", 
      "troyscore":"32", 
      "vsscore":"41", 
     }, 
     { 
      "versus":"Sunny Hills", 
      "homeaway":"Away", 
      "month":"March", 
      "date":"28", 
      "year":"2012", 
      "troyscore":"20", 
      "vsscore":"17", 
     }] 
    }, 
    { 
     "level": "jv", 
     "games":[ 
     { 
      "versus":"Sonora", 
      "homeaway":"Home", 
      "month":"January", 
      "date":"20", 
      "year":"2012", 
      "troyscore":"15", 
      "vsscore":"21", 
     }, 
     { 
      "versus":"Valencia", 
      "homeaway":"Away", 
      "month":"April", 
      "date":"30", 
      "year":"2012", 
      "troyscore":"40", 
      "vsscore":"1", 
     }] 
    }] 
}, 
{ 
    "gender":"girls", 
    "levels":[ 
    { 
     "level": "varsity", 
     "games":[ 
     { 
      "versus":"Acacia", 
      "homeaway":"Home", 
      "month":"February", 
      "date":"27", 
      "year":"2012", 
      "troyscore":"32", 
      "vsscore":"41", 
     }, 
     { 
      "versus":"LV", 
      "homeaway":"Away", 
      "month":"March", 
      "date":"28", 
      "year":"2012", 
      "troyscore":"20", 
      "vsscore":"17", 
     }] 
    }, 
    { 
     "level": "jv", 
     "games":[ 
     { 
      "versus":"Commonwealth", 
      "homeaway":"Home", 
      "month":"January", 
      "date":"20", 
      "year":"2012", 
      "troyscore":"15", 
      "vsscore":"21", 
     }, 
     { 
      "versus":"Xishan", 
      "homeaway":"Away", 
      "month":"April", 
      "date":"30", 
      "year":"2012", 
      "troyscore":"40", 
      "vsscore":"1", 
     }] 
    }] 
}] 
} 
+0

儘管URLs很像文件系統路徑,但AFAIK並不是必需的 - 而且服務器可能不知道(或者出於安全原因,可能會主動忘記)「..」表示「父目錄」。你有沒有嘗試一個絕對的網址? – cHao 2012-02-12 23:44:14

+0

如果你在'$(「#add」)...'前面加上'console.log(data)',你會在控制檯中看到什麼? – fncomp 2012-02-12 23:48:09

回答

3

您的每個內的對象的最終性質都尾隨逗號,使你的JSON格式錯誤。試試將它粘貼到jsonlint.com。它會在每一個物體的最後物業上釀酒。

{ 
     "versus":"Fullerton", 
     "homeaway":"Home", 
     "month":"February", 
     "date":"27", 
     "year":"2012", 
     "troyscore":"32", 
     "vsscore":"41", <-- borked JSON 
    }, 

投訴:如果不嚴格合式返回的JSON

Parse error on line 67: 
...     },     
-----------------------^ 
Expecting 'STRING' 

$.getJSON將靜默失敗。我想從文檔中提供一個參考,但看起來jQuery網站已經關閉了.... bleh。

+0

這將仍然在非IE瀏覽器中工作,它不能成爲不工作的原因。 – ShankarSangoli 2012-02-12 23:48:52

+0

這不一定是關於瀏覽器,而是關於只接受格式良好的JSON的jQuery。 – karim79 2012-02-12 23:51:54

+0

@ShankarSangoli:這是JSON,而不是JavaScript。他們是不同的規格,不同的規則。 http://jsfiddle.net/5n8Qz/ – 2012-02-12 23:52:42