您好我想获得Steam用户的个性 我在.json格式的文件中存储了数据。
{
"response": {
"players": [
{
"steamid": "76561198137714668",
"communityvisibilitystate": 3,
"profilestate": 1,
"personaname": "UareBugged",
"lastlogoff": 1418911040,
"commentpermission": 1,
"profileurl": "http://steamcommunity.com/id/uarenotbest/",
"avatar": "http://cdn.akamai.steamstatic.com/steamcommunity/public/images/avatars/da/daece8a16d866ef9bd03ddc4aa365c5862af1c21.jpg",
"avatarmedium": "http://cdn.akamai.steamstatic.com/steamcommunity/public/images/avatars/da/daece8a16d866ef9bd03ddc4aa365c5862af1c21_medium.jpg",
"avatarfull": "http://cdn.akamai.steamstatic.com/steamcommunity/public/images/avatars/da/daece8a16d866ef9bd03ddc4aa365c5862af1c21_full.jpg",
"personastate": 1,
"realname": "Michal Šlesár",
"primaryclanid": "103582791436765601",
"timecreated": 1400861961,
"personastateflags": 0,
"loccountrycode": "SK",
"locstatecode": "08"
}
]
}
}
我想让personaname变为变量,但它什么都不做,变量是空的 我认为json_decode不起作用,但我真的不知道。
$pname = json_decode(file_get_contents("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v002/?key=KEYCONSORED&Steamids={$_SESSION['T2SteamID64']}"));
echo $pname['response']['players']['personaname'];
回声是空的
答案 0 :(得分:9)
玩家是阵列:
$pname['response']['players'][0]['personaname'];
答案 1 :(得分:3)
这里有几个错误。
让我逐一解释一下如何找到PHP JSON解码/编码的常见错误。
首先,您的JSON无效,最后错过了结尾}
。
更新:就在@tftd评论之后我看到你错误地格式化了你的代码,但无论如何,让我解释一下如何找到问题,因为这不应该像在PHP中那样微不足道。其他错误仍然是有效的。
要检查json_decode无效的原因,请使用json_last_error
:它会返回错误编号,这意味着:
0 = JSON_ERROR_NONE = "No error has occurred"
1 = JSON_ERROR_DEPTH = "The maximum stack depth has been exceeded"
2 = JSON_ERROR_STATE_MISMATCH = "Invalid or malformed JSON"
3 = JSON_ERROR_CTRL_CHAR = "Control character error, possibly incorrectly encoded"
4 = JSON_ERROR_SYNTAX = "Syntax error"
5 = JSON_ERROR_UTF8 = "Malformed UTF-8 characters, possibly incorrectly encode"
6 = JSON_ERROR_RECURSION = "One or more recursive references in the value to be encoded"
7 = JSON_ERROR_INF_OR_NAN = "One or more NAN or INF values in the value to be encoded"
8 = JSON_ERROR_UNSUPPORTED_TYPE = "A value of a type that cannot be encoded was given"
在您的情况下,它正在返回4
。所以,我在http://jsonlint.com验证了您的JSON,并在最后找到了遗失的}
。
如果您想要访问一个$pname
数组,则需要将json_decode
行放到:
$pname = json_decode(file_get_contents("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v002/?key=KEYCONSORED&Steamids={$_SESSION['T2SteamID64']}"), true);
请注意true
方法的最后一个参数json_decode
。根据{{3}},当true
时,返回的对象将被转换为关联数组。
修正了您的JSON和json_decode
调用,我们可以看到players
是一个数组。因此,如果您想要阅读第一个播放器,请使用:
$pname['response']['players'][0]
我没有从网址上阅读,因此我使用了documentation:
<?php
$content = <<<EOD
{
"response": {
"players": [
{
"steamid": "76561198137714668",
"communityvisibilitystate": 3,
"profilestate": 1,
"personaname": "UareBugged",
"lastlogoff": 1418911040,
"commentpermission": 1,
"profileurl": "http://steamcommunity.com/id/uarenotbest/",
"avatar": "http://cdn.akamai.steamstatic.com/steamcommunity/public/images/avatars/da/daece8a16d866ef9bd03ddc4aa365c5862af1c21.jpg",
"avatarmedium": "http://cdn.akamai.steamstatic.com/steamcommunity/public/images/avatars/da/daece8a16d866ef9bd03ddc4aa365c5862af1c21_medium.jpg",
"avatarfull": "http://cdn.akamai.steamstatic.com/steamcommunity/public/images/avatars/da/daece8a16d866ef9bd03ddc4aa365c5862af1c21_full.jpg",
"personastate": 1,
"realname": "Michal Šlesár",
"primaryclanid": "103582791436765601",
"timecreated": 1400861961,
"personastateflags": 0,
"loccountrycode": "SK",
"locstatecode": "08"
}
]
}
}
EOD;
$pname = json_decode($content, true);
echo $pname['response']['players'][0]['personaname'];
这将按预期输出UareBugged
。