2017-08-23 2 views
0

何か助けを非常に感謝します。jsonファイルからjquery変数を作成する方法は?

基本的に私はmashapeのapiを使用していますが、私はJSONファイルの初心者です。

私がしたいことは、各チームの合計ポイントのjquery変数のロードを作成することです。

チームがテーブル内の位置に応じてJSONファイルの位置を変更することに注意してください。

以下は、私のjqueryコード(認証コードなし)とJSONファイルです。

$.ajax({ 
url: 'https://heisenbug-premier-league-live-scores-v1.p.mashape.com/api/premierleague/table', 
type: 'GET', 
data: {}, 
dataType: 'json', 
success: function(data) { 
    $(data.records).each(function(index, value) { 



    }); 

    console.dir((data.source)); 
}, 
error: function(err) { alert(err); }, 
beforeSend: function(xhr) { 
xhr.setRequestHeader("X-Mashape-Authorization", 
"Auth Code"); 
} 
}); 

そしてJSONファイル。

{ 
"records": [ 
{ 
    "team": "Manchester City", 
    "played": 10, 
    "win": 8, 
    "draw": 0, 
    "loss": 2, 
    "goalsFor": 29, 
    "goalsAgainst": 12, 
    "points": 24 
}, 
{ 
    "team": "Arsenal", 
    "played": 10, 
    "win": 7, 
    "draw": 2, 
    "loss": 1, 
    "goalsFor": 16, 
    "goalsAgainst": 6, 
    "points": 23 
}, 
{ 
    "team": "Tottenham", 
    "played": 10, 
    "win": 5, 
    "draw": 4, 
    "loss": 1, 
    "goalsFor": 18, 
    "goalsAgainst": 7, 
    "points": 19 
}, 
{ 
    "team": "Leicester", 
    "played": 10, 
    "win": 5, 
    "draw": 4, 
    "loss": 1, 
    "goalsFor": 16, 
    "goalsAgainst": 13, 
    "points": 19 
}, 
{ 
    "team": "Manchester United", 
    "played": 10, 
    "win": 5, 
    "draw": 4, 
    "loss": 1, 
    "goalsFor": 12, 
    "goalsAgainst": 4, 
    "points": 19 
}, 
{ 
    "team": "West Ham", 
    "played": 10, 
    "win": 4, 
    "draw": 4, 
    "loss": 2, 
    "goalsFor": 16, 
    "goalsAgainst": 12, 
    "points": 16 
}, 
{ 
    "team": "Liverpool", 
    "played": 9, 
    "win": 4, 
    "draw": 3, 
    "loss": 2, 
    "goalsFor": 11, 
    "goalsAgainst": 11, 
    "points": 15 
}, 
{ 
    "team": "Norwich", 
    "played": 10, 
    "win": 4, 
    "draw": 3, 
    "loss": 3, 
    "goalsFor": 12, 
    "goalsAgainst": 10, 
    "points": 15 
}, 
{ 
    "team": "Southampton", 
    "played": 10, 
    "win": 4, 
    "draw": 2, 
    "loss": 4, 
    "goalsFor": 17, 
    "goalsAgainst": 13, 
    "points": 14 
}, 
{ 
    "team": "Chelsea", 
    "played": 10, 
    "win": 4, 
    "draw": 2, 
    "loss": 4, 
    "goalsFor": 15, 
    "goalsAgainst": 14, 
    "points": 14 
}, 
{ 
    "team": "West Bromwich Albion", 
    "played": 11, 
    "win": 4, 
    "draw": 2, 
    "loss": 5, 
    "goalsFor": 14, 
    "goalsAgainst": 17, 
    "points": 14 
}, 
{ 
    "team": "Crystal Palace", 
    "played": 11, 
    "win": 4, 
    "draw": 2, 
    "loss": 5, 
    "goalsFor": 12, 
    "goalsAgainst": 12, 
    "points": 14 
}, 
{ 
    "team": "Watford", 
    "played": 11, 
    "win": 4, 
    "draw": 2, 
    "loss": 5, 
    "goalsFor": 11, 
    "goalsAgainst": 10, 
    "points": 14 
}, 
{ 
    "team": "Stoke", 
    "played": 9, 
    "win": 4, 
    "draw": 1, 
    "loss": 4, 
    "goalsFor": 10, 
    "goalsAgainst": 9, 
    "points": 13 
}, 
{ 
    "team": "Swansea", 
    "played": 10, 
    "win": 3, 
    "draw": 4, 
    "loss": 3, 
    "goalsFor": 9, 
    "goalsAgainst": 12, 
    "points": 13 
}, 
{ 
    "team": "Everton", 
    "played": 11, 
    "win": 3, 
    "draw": 4, 
    "loss": 4, 
    "goalsFor": 23, 
    "goalsAgainst": 20, 
    "points": 13 
}, 
{ 
    "team": "Sunderland", 
    "played": 10, 
    "win": 3, 
    "draw": 2, 
    "loss": 5, 
    "goalsFor": 12, 
    "goalsAgainst": 11, 
    "points": 11 
}, 
{ 
    "team": "Bournemouth", 
    "played": 9, 
    "win": 2, 
    "draw": 4, 
    "loss": 3, 
    "goalsFor": 10, 
    "goalsAgainst": 13, 
    "points": 10 
}, 
{ 
    "team": "Newcastle United", 
    "played": 10, 
    "win": 2, 
    "draw": 4, 
    "loss": 4, 
    "goalsFor": 14, 
    "goalsAgainst": 14, 
    "points": 10 
}, 
{ 
    "team": "Aston Villa", 
    "played": 9, 
    "win": 0, 
    "draw": 3, 
    "loss": 6, 
    "goalsFor": 6, 
    "goalsAgainst": 13, 
    "points": 3 
} 
] 
} 

Thank You Very Much!

+1

jquery変数は何を意味しますか?あなたは解析されたjsonを使うことはできませんか? –

+0

ここで何をしようとしているのか分かりません。jqueryを使用してこのデータを表示したいのですか?リーグテーブルのように見えますか? – Keith

+0

"jQuery変数"のようなものはありません。 「JSONからデータをフェッチして変数に格納するためにjQueryを使用する」のようなものでしょうか? –

答えて

0

オブジェクトを作成して各チームの合計ポイントを追加するには、キーが各チームの名前であり、値が各チームの合計ポイントであるマップ(キー/値)を作成する必要があります。

私はMyJSONにあなたのデータをホストしています。

ここでは、リモートサーバーからデータを取得する機能だし、各チームの合計ポイントを含むオブジェクトを初期化します

function getData() { 
    // https://api.myjson.com/bins/1c6utx 
    var teamScores = {}; 

    $.get("https://api.myjson.com/bins/1c6utx", function(data, status){ 
    $.each(data.records, function(index, value) { 
     if (!teamScores[value.team]) { 
     teamScores[value.team] = 0; 
     } 

     teamScores[value.team] += value.points; 
    }); 

    console.log(JSON.stringify(teamScores)); 
    }); 
} 

/* Output: 
{"Manchester City":24,"Arsenal":23,"Tottenham":19,"Leicester":19,"Manchester United":19,"West Ham":16,"Liverpool":15,"Norwich":15,"Southampton":14,"Chelsea":14,"West Bromwich Albion":14,"Crystal Palace":14,"Watford":14,"Stoke":13,"Swansea":13,"Everton":13,"Sunderland":11,"Bournemouth":10,"Newcastle United":10,"Aston Villa":3} 
*/ 

あなたがアクションでそれを見るために、このfiddleを確認することができます。お役に立てれば!

+0

あなたの助けをありがとう、痛みをおかけして申し訳ありませんが、どのように私はチームの後ろに名前が付けられた変数を作成するのですか? –

+0

@BrandonRobertsオブジェクト 'teamScores'には各ティーチの属性が含まれ、その値は各チームの合計ポイントとなります。たとえば、 'teamScores ['Manchester City']'を実行すると、そのチームの合計得点が得られます。あなたが何を意味するのか正確にはわからない...各チームの変数を定義しようとしていますか?チームのリストは修正されていますか? – mscheker

関連する問題