从url解析JSON以在html

时间:2017-09-21 01:45:56

标签: javascript html json

我在网址中有一个json文件,显示如下

[{"tier":"SILVER","leagueName":"Tryndamere's Wizards","queueType":"RANKED_SOLO_5x5","playerOrTeamId":"91248124",    "playerOrTeamName":"NunoC99","leaguePoints":18,"wins":411,"losses":430,"rank":"II","veteran":true,"inactive":false,"freshBlood":false,"hotStreak":false},
{"tier":"BRONZE","leagueName":"Kennen's Adventurers","queueType":"RANKED_FLEX_SR","playerOrTeamId":"91248124",    "playerOrTeamName":"NunoC99","leaguePoints":2,"wins":28,"losses":41,"rank":"III","veteran":false,"inactive":false,"freshBlood":false,"hotStreak":false}]7

我想通过javascript解析它,并用它来显示html中的等级,等级和联盟点。 我是新手,我无法弄清楚如何将json解析为可用的变量以显示在html文件中。如果可以,请帮忙。

2 个答案:

答案 0 :(得分:1)

您可以使用jquery从url

获取json
For example, I would like:

       COL1      COL2
1     name1    score1
2    state1   rating1
3     name2    score2
4    state2   rating2

To become:

      COL1      COL2     COL3      COL4
1    name1    score1   state1   rating1
2    name2    score2   state2   rating2

答案 1 :(得分:0)

请参阅问题How to get JSON from URL in Javascript以访问URL中的数据。然后,您可以使用循环遍历数据:

var data = [{"tier":"SILVER","leagueName":"Tryndamere's Wizards","queueType":"RANKED_SOLO_5x5","playerOrTeamId":"91248124",    "playerOrTeamName":"NunoC99","leaguePoints":18,"wins":411,"losses":430,"rank":"II","veteran":true,"inactive":false,"freshBlood":false,"hotStreak":false}, {"tier":"BRONZE","leagueName":"Kennen's Adventurers","queueType":"RANKED_FLEX_SR","playerOrTeamId":"91248124",    "playerOrTeamName":"NunoC99","leaguePoints":2,"wins":28,"losses":41,"rank":"III","veteran":false,"inactive":false,"freshBlood":false,"hotStreak":false}]

for (var i = 0; i < data.length; i++) {
  // Within the loop you can access each field of each object
  // as shown below
  // data[i].tier
  // data[i].leagueName
  // data[i].queueType
  // data[i].playerOrTeamId
  // data[i].leaguePoints
  // data[i].wins
  // data[i].losses
  // data[i].rank
  // data[i].veteran
  // data[i].freshBlood
  // data[i].hotStreak
  // data[i].inactive
}

您可以使用document.createElement("TAG_NAME")document.appendChild(childElement)等方法将数据插入HTML文档。