拆分字符串以查看统计信息

时间:2015-05-24 01:09:46

标签: json string split

如果我想将json中的统计信息转换为可读列表,我该怎么做?

例如,

{
    "player_stat_summary": [
        {
            "id": 1,
            "stats": {
                "id": 1,
                "average_node_capture_assist": 0,
                "max_node_neutralize_assist": 0,
                "total_minion_kills": 0,
                "max_champions_killed": 0,
                "total_champion_kills": 0,
                "average_champions_killed": 0,
                "average_num_deaths": 0,
                "max_node_capture": 0,
                "max_objective_player_score": 0,
                "total_neutral_minions_killed": 0,
                "max_assists": 0,
                "average_combat_player_score": 0,
                "max_node_capture_assist": 0,
                "average_objective_player_score": 0,
                "max_team_objective": 0,
                "total_assists": 0,
                "average_node_capture": 0,
                "average_total_player_score": 0,
                "average_team_objective": 0,
                "average_node_neutralize": 0,
                "max_node_neutralize": 0,
                "average_node_neutralize_assist": 0,
                "average_assists": 0,
                "max_total_player_score": 0,
                "max_combat_player_score": 0,
                "total_turrets_killed": 0,
                "total_node_neutralize": 0,
                "total_node_capture": 0
            },
            "player_stat_summary_type": "Coop",
            "wins": 100,
            "losses": 0
        }
    ],
    "time_stamp": "2015-05-22T15:54:43.069814Z",
    "summoner_id": 0
}

2 个答案:

答案 0 :(得分:1)

你不会说你正在使用的语言。

假设JS。

您可以使用JSON.parse(text)

因此假设您的JSON字符串存储在名为text

的变量中
var stats= JSON.parse(text);

console.log(stats.player_stat_summary[0].id);
    > 1

然而,它看起来根本不是一个字符串,但实际上已经是一个对象了。您只是没有将其分配给变量。如果是这种情况,只需将其分配给变量并像上面那样使用它,不需要JSON.parse(text);

e.g。

stats={....};
console.log(stats.player_stat_summary[0].id);
    > 1

答案 1 :(得分:0)

如果您想迭代统计数据并发出HTML列表,您可以执行以下操作:



var stats = {
    "player_stat_summary": [
        {
            "id": 1,
            "stats": {
                "id": 1,
                "average_node_capture_assist": 0,
                "max_node_neutralize_assist": 0,
                "total_minion_kills": 0,
                "max_champions_killed": 0,
                "total_champion_kills": 0,
                "average_champions_killed": 0,
                "average_num_deaths": 0,
                "max_node_capture": 0,
                "max_objective_player_score": 0,
                "total_neutral_minions_killed": 0,
                "max_assists": 0,
                "average_combat_player_score": 0,
                "max_node_capture_assist": 0,
                "average_objective_player_score": 0,
                "max_team_objective": 0,
                "total_assists": 0,
                "average_node_capture": 0,
                "average_total_player_score": 0,
                "average_team_objective": 0,
                "average_node_neutralize": 0,
                "max_node_neutralize": 0,
                "average_node_neutralize_assist": 0,
                "average_assists": 0,
                "max_total_player_score": 0,
                "max_combat_player_score": 0,
                "total_turrets_killed": 0,
                "total_node_neutralize": 0,
                "total_node_capture": 0
            },
            "player_stat_summary_type": "Coop",
            "wins": 100,
            "losses": 0
        }
    ],
    "time_stamp": "2015-05-22T15:54:43.069814Z",
    "summoner_id": 0
}

function emitStats(s) {
  // create a new UL
  var list = document.createElement('ul');
  
  // iterate over the properties
  Object.keys(s).forEach(function(statName) {
    // create an LI for the property
    var el = document.createElement('li');
    // fill it with the stat name and value
    el.innerHTML = statName.replace(/_/g,' ') + ": " + s[statName];
    // add it to the UL
    list.appendChild(el);
  });
  // add the UL to the document.
  document.getElementById('content').appendChild(list);
}

// loop over each entry in stats.player_stat_summary
for(var i = 0; i < stats.player_stat_summary.length; i++ ) {
  // invoke emitStats, passing the entry's stats property.
  emitStats(stats.player_stat_summary[i].stats);
}
&#13;
<div id="content"></div>
&#13;
&#13;
&#13;