1

大家好,我正在尝试从本网站的 Web 服务获取此 json 文件:“http://172.20.192.218:8080/DC_Local/rest/datacorrelation?type=/soccer/football_team&limit=20&callback=?”

但是我得到了这个“Uncaught SyntaxError:Unexpected token”但我不知道有什么问题。

这是我调用 json 文件的代码:

var pts = [];
    $.getJSON("http://172.20.192.218:8080/DC_Local/rest/datacorrelation?type=/soccer/football_team&limit=20&callback=?", function(data) {
        //JSON.parse(data);

        $.each(data, function(i,v){
            pts.push([v.topic, v.audience.length]);
        });
     });

这是我想要获取的 json 文件:

{"data":[{"id":"/en/arsenal_fc","topic":"Arsenal F.C.","audience":[],"type":"/soccer/football_team"},{"id":"/en/ajax_amsterdam","topic":"Ajax Amsterdam","audience":[],"type":"/soccer/football_team"},{"id":"/en/az_alkmaar","topic":"AZ Alkmaar","audience":[],"type":"/soccer/football_team"},{"id":"/en/as_roma","topic":"A.S. Roma","audience":[],"type":"/soccer/football_team"},{"id":"/en/ac_milan","topic":"A.C. Milan","audience":[],"type":"/soccer/football_team"},{"id":"/en/acf_fiorentina","topic":"ACF Fiorentina","audience":[],"type":"/soccer/football_team"},{"id":"/en/bristol_city_fc","topic":"Bristol City F.C.","audience":[],"type":"/soccer/football_team"},{"id":"/en/charlton_athletic_fc","topic":"Charlton Athletic F.C.","audience":[],"type":"/soccer/football_team"},{"id":"/en/ac_chievoverona","topic":"A.C. Chievo Verona","audience":[],"type":"/soccer/football_team"},{"id":"/en/chelsea_fc","topic":"Chelsea F.C.","audience":[{"information":[{"category":"Athlete","source":"Frank Lampard"}],"username":"Tan Jercia","userid":"100003921730958"},{"information":[{"category":"Athlete","source":"Didier Drogba"},{"category":"Athlete","source":"Frank Lampard"},{"category":"Professional sports team","source":"Chelsea Football Club"},{"category":"favorite_athletes","source":"Peter Cech"},{"category":"favorite_athletes","source":"Didier Drogba"},{"category":"favorite_athletes","source":"Frank Lampard"}],"username":"Ang Peterson","userid":"100003914111287"}],"type":"/soccer/football_team"},{"id":"/en/england_national_football_team","topic":"England national football team","audience":[],"type":"/soccer/football_team"},{"id":"/en/fulham_fc","topic":"Fulham F.C.","audience":[],"type":"/soccer/football_team"},{"id":"/en/f_c_den_bosch","topic":"F. C. Den Bosch","audience":[],"type":"/soccer/football_team"},{"id":"/en/hellas_verona_fc","topic":"Hellas Verona F.C.","audience":[],"type":"/soccer/football_team"},{"id":"/en/fc_internazionale_milano","topic":"F.C. Internazionale Milano","audience":[],"type":"/soccer/football_team"},{"id":"/en/juventus_fc","topic":"Juventus F.C.","audience":[],"type":"/soccer/football_team"},{"id":"/en/liverpool_fc","topic":"Liverpool F.C.","audience":[],"type":"/soccer/football_team"},{"id":"/en/luton_town_fc","topic":"Luton Town F.C.","audience":[],"type":"/soccer/football_team"},{"id":"/en/manchester_united_fc","topic":"Manchester United F.C.","audience":[{"information":[{"category":"Professional sports team","source":"Manchester United"}],"username":"Tan Jercia","userid":"100003921730958"}],"type":"/soccer/football_team"},{"id":"/en/plymouth_argyle_fc","topic":"Plymouth Argyle F.C.","audience":[],"type":"/soccer/football_team"}]}
4

1 回答 1

1

你必须写$.each(data.data, function(i,v){而不是$.each(data, function(i,v){.

样本:

<!DOCTYPE html>
<html>
    <head>
        <title>Add many Elements</title>
        <script src="jquery.js"></script>
        <script>
            $(document).ready(function() {
            var dataString = '{"data":[{"id":"/en/arsenal_fc","topic":"Arsenal F.C.","audience":[],"type":"/soccer/football_team"},{"id":"/en/ajax_amsterdam","topic":"Ajax Amsterdam","audience":[],"type":"/soccer/football_team"},{"id":"/en/az_alkmaar","topic":"AZ Alkmaar","audience":[],"type":"/soccer/football_team"},{"id":"/en/as_roma","topic":"A.S. Roma","audience":[],"type":"/soccer/football_team"},{"id":"/en/ac_milan","topic":"A.C. Milan","audience":[],"type":"/soccer/football_team"},{"id":"/en/acf_fiorentina","topic":"ACF Fiorentina","audience":[],"type":"/soccer/football_team"},{"id":"/en/bristol_city_fc","topic":"Bristol City F.C.","audience":[],"type":"/soccer/football_team"},{"id":"/en/charlton_athletic_fc","topic":"Charlton Athletic F.C.","audience":[],"type":"/soccer/football_team"},{"id":"/en/ac_chievoverona","topic":"A.C. Chievo Verona","audience":[],"type":"/soccer/football_team"},{"id":"/en/chelsea_fc","topic":"Chelsea F.C.","audience":[{"information":[{"category":"Athlete","source":"Frank Lampard"}],"username":"Tan Jercia","userid":"100003921730958"},{"information":[{"category":"Athlete","source":"Didier Drogba"},{"category":"Athlete","source":"Frank Lampard"},{"category":"Professional sports team","source":"Chelsea Football Club"},{"category":"favorite_athletes","source":"Peter Cech"},{"category":"favorite_athletes","source":"Didier Drogba"},{"category":"favorite_athletes","source":"Frank Lampard"}],"username":"Ang Peterson","userid":"100003914111287"}],"type":"/soccer/football_team"},{"id":"/en/england_national_football_team","topic":"England national football team","audience":[],"type":"/soccer/football_team"},{"id":"/en/fulham_fc","topic":"Fulham F.C.","audience":[],"type":"/soccer/football_team"},{"id":"/en/f_c_den_bosch","topic":"F. C. Den Bosch","audience":[],"type":"/soccer/football_team"},{"id":"/en/hellas_verona_fc","topic":"Hellas Verona F.C.","audience":[],"type":"/soccer/football_team"},{"id":"/en/fc_internazionale_milano","topic":"F.C. Internazionale Milano","audience":[],"type":"/soccer/football_team"},{"id":"/en/juventus_fc","topic":"Juventus F.C.","audience":[],"type":"/soccer/football_team"},{"id":"/en/liverpool_fc","topic":"Liverpool F.C.","audience":[],"type":"/soccer/football_team"},{"id":"/en/luton_town_fc","topic":"Luton Town F.C.","audience":[],"type":"/soccer/football_team"},{"id":"/en/manchester_united_fc","topic":"Manchester United F.C.","audience":[{"information":[{"category":"Professional sports team","source":"Manchester United"}],"username":"Tan Jercia","userid":"100003921730958"}],"type":"/soccer/football_team"},{"id":"/en/plymouth_argyle_fc","topic":"Plymouth Argyle F.C.","audience":[],"type":"/soccer/football_team"}]}'
                var pts = [];
        var data = JSON.parse(dataString);

        $.each(data.data, function(index,data){
            pts.push([data.topic, data.audience.length]);
        });
            });
        </script>
    </head>
    <body>
        <div class="testDiv">
            <div class="testDivPos"></div>
        </div>
    </body>
</html>
于 2013-01-15T08:47:37.913 回答