我正在尝试解析来自Steam Web API的传入JSON,以便显示游戏中的特定统计信息。在这种情况下,游戏是Rust。
从API返回的JSON:
{
"playerstats": {
"steamID":"76561198288875739",
"gameName":"",
"stats":[
{"name":"deaths","value":268},
{"name":"bullet_fired","value":11370},
{"name":"arrow_fired","value":856},
{"name":"item_drop","value":3850},
{"name":"blueprint_studied","value":293},
{"name":"death_suicide","value":103},
{"name":"death_fall","value":1},
{"name":"death_selfinflicted","value":8},
{"name":"kill_player","value":296},
{"name":"bullet_hit_player","value":1171},
{"name":"arrow_hit_entity","value":33},
{"name":"harvest.stones","value":106299},
{"name":"bullet_hit_entity","value":2484},
{"name":"harvest.cloth","value":4426},
{"name":"harvest.wood","value":379165},
{"name":"arrow_hit_building","value":90},
{"name":"kill_bear","value":34},
{"name":"kill_boar","value":46},
{"name":"kill_stag","value":31},
{"name":"kill_chicken","value":8},
{"name":"kill_horse","value":37},
{"name":"kill_wolf","value":26},
{"name":"headshot","value":364},
{"name":"arrow_hit_boar","value":15},
{"name":"arrow_hit_bear","value":68},
{"name":"arrow_hit_wolf","value":18},
{"name":"arrow_hit_stag","value":4},
{"name":"arrow_hit_chicken","value":1},
{"name":"bullet_hit_building","value":2455},
{"name":"arrow_hit_horse","value":25},
{"name":"arrow_hit_player","value":156},
{"name":"death_entity","value":18},
{"name":"death_wolf","value":1},
{"name":"death_bear","value":7},
{"name":"shotgun_fired","value":533},
{"name":"shotgun_hit_building","value":30},
{"name":"shotgun_hit_player","value":61},
{"name":"shotgun_hit_horse","value":12},
{"name":"bullet_hit_bear","value":168},
{"name":"shotgun_hit_entity","value":95},
{"name":"bullet_hit_horse","value":70},
{"name":"bullet_hit_stag","value":20},
{"name":"bullet_hit_wolf","value":31},
{"name":"bullet_hit_boar","value":14},
{"name":"bullet_hit_sign","value":9},
{"name":"wounded","value":137},
{"name":"wounded_assisted","value":53},
{"name":"wounded_healed","value":27},
{"name":"bullet_hit_playercorpse","value":96},
{"name":"bullet_hit_corpse","value":87},
{"name":"INVENTORY_OPENED","value":5292},
{"name":"CRAFTING_OPENED","value":72},
{"name":"MAP_OPENED","value":483},
{"name":"CUPBOARD_OPENED","value":78},
{"name":"ITEM_EXAMINED","value":6},
{"name":"comfort_duration","value":7701.56640625},
{"name":"calories_consumed","value":15861.1572265625},
{"name":"water_consumed","value":4199.1533203125},
{"name":"radiation_exposure_duration","value":799.53271484375},
{"name":"cold_exposure_duration","value":8946.0244140625},
{"name":"hot_exposure_duration","value":2202.734619140625},
{"name":"melee_strikes","value":1940},
{"name":"melee_thrown","value":47},
{"name":"placed_blocks","value":164},
{"name":"upgraded_blocks","value":1050},
{"name":"arrows_shot","value":250},
{"name":"seconds_speaking","value":79.7517318725585938},
{"name":"acquired_lowgradefuel","value":748},
{"name":"acquired_metal.ore","value":24883},
{"name":"acquired_scrap","value":494},
{"name":"topology_road_duration","value":785},
{"name":"destroyed_barrels","value":113}
],
"achievements":[
{"name":"CONSTRUCT_BASE","achieved":1},
{"name":"UPGRADE_BASE","achieved":1},
{"name":"COLLECT_50_LGF","achieved":1},
{"name":"COLLECT_300_METAL_ORE","achieved":1},
{"name":"VISIT_ROAD","achieved":1},
{"name":"COLLECT_65_SCRAP","achieved":1},
{"name":"DESTROY_10_BARRELS","achieved":1}
]
}
}
我无法使用此数据显示所需的统计信息。例如,我想显示总玩家杀戮("name":"kill_player","value":296
)
我将如何设法做到这一点?我以前解析过一些简单的JSON但没有这么复杂。
感谢帮助!
编辑
通过这样做,我设法获得了所有值的所有名称:
$rust_info_result = json_decode($rust_info_result);
$rust_info_result = $rust_info_result->playerstats->stats;
foreach($rust_info_result as $r2)
{
echo $r2->name; echo " "; echo $r2->value; echo "<br>";
}
但我仍然无法找到获取特定统计数据的方法。比如kill_player。
编辑#2
我为了让这个工作得到了应对。对于可能和我有同样问题的其他人,我发现以下方法对我来说非常合适:
$rust_info_result = json_decode($rust_info_result);
$rust_info_result = $rust_info_result->playerstats->stats;
$stats = [];
foreach ($rust_info_result as $r2)
{
$stats[$r2->name] = $r2->value;
}
echo "Player Kills: " . $stats['kill_player'];
答案 0 :(得分:0)
您需要遍历项目数组以找到具有您正在寻找的name
的项目。不幸的是,没有内置功能可以帮助你。
<?php
$json = '{
"playerstats": {
"steamID":"76561198288875739",
"gameName":"",
"stats":[
{"name":"melee_strikes","value":1940},
{"name":"melee_thrown","value":47},
{"name":"placed_blocks","value":164},
{"name":"upgraded_blocks","value":1050},
],
"achievements":[
{"name":"CONSTRUCT_BASE","achieved":1},
{"name":"UPGRADE_BASE","achieved":1},
]
}
}';
function getStat(string $name, array $from): float
{
foreach ($from['playerstats']['stats'] as $item) {
if (isset($item['name']) && isset($item['value']) && $item['name'] === $name) {
return $item['value'];
}
}
return -1;
}
function getAchievement(string $name, array $from): float
{
foreach ($from['playerstats']['achievements'] as $item) {
if (isset($item['name']) && isset($item['achieved']) && $item['name'] === $name) {
return $item['achieved'];
}
}
return -1;
}
$dec = json_decode($json, true);
echo 'upgraded_blocks: ' . getStat('upgraded_blocks', $dec) . PHP_EOL;
echo 'CONSTRUCT_BASE: ' . getAchievement('CONSTRUCT_BASE', $dec) . PHP_EOL;
我写了一些非常基本的帮助函数来完成这项工作。如果您输入不存在的统计或成就名称(或者如果它不是用大写/小写字母书写的话),他们将返回-1
。