我有一点麻烦在这里...抓住无名(?)的JSON数据与PHP
我试图从一个JSON文件here,抓住一些值,它可以被格式化here。
JSON文件看起来是这样的:
{
"type": "success",
"message": "OK",
"data": {
"mainWeaponStats": [
{
"category": "Machine guns",
"timeEquipped": 3507,
"startedWith": null,
"code": "mgRPK",
"headshots": 18,
"name": "RPK",
"kills": 100,
"deaths": null,
},
{
"category": "Handheld weapons",
"timeEquipped": 5452,
"startedWith": null,
"code": "wahUGL",
"headshots": 1,
"name": "Underslung Launcher",
"kills": 108,
"deaths": null,
},
{
"category": "Sniper rifles",
"timeEquipped": 307,
"startedWith": null,
"code": "srMK11",
"headshots": 0,
"name": "MK11",
"kills": 2,
"deaths": null,
},
等。
我想抓住这些物品之一的杀戮。含义我想给一个像“Underslung Launcher”这样的参数,并返回108. 在这种情况下,“Underslung Launcher”。 我正在寻找这样的代码:
$gamemode = $decode['data']['topStats']['mapMode'];
但是,如果有人知道一个更好的办法,请告诉我。 由于列表中的项目没有“名称”,不像“数据”&“mainWeaponStats”,我无法真正弄清楚如何做到这一点。
编辑: 这是相关代码至今:
$weaponstats = "http://battlelog.battlefield.com/bf3/weaponsPopulateStats/" . $bf3id . "/1/";
$content = file_get_contents($weaponstats);
$decode = json_decode($content, true);
$mainweaponstats = $decode['data']['mainWeaponStats'];
正如你所看到的,我有一个很难学习JSON。 我正试图读取它,但截至目前,我无法弄清楚这一点。
我真的不知道我会如何去做,因为我试图找到的值是在同一组中。
如果我理解正确的,你想给像悬挂式启动参数,返回108? – alpera 2012-08-16 13:54:31
是的,这就是我想要做的。 – 2012-08-16 13:56:13
'mainWeaponStats'是一个数组。循环,并检查每一个,看它的名字是否匹配,然后返回杀死。 – 2012-08-16 13:56:51