1

我一直在玩弄使用 JSON 格式的 Steam Web API。我一直在尝试打印 API 给出的数组输出。

<?php
    $id = $_GET['id'];
    $key = 'xxx';

    $link = file_get_contents('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=' . $key . '&steamids=' . $id . '&format=json');
    $profile_info = json_decode($link);

    $json_response = json_encode($profile_info->response);
    print($json_response['steamid']);
?>

该密钥显然被 Steam 生成器提供给我的密钥所取代。但是这个代码片段我只返回{它应该返回的字符76561197989628470,即我的 steamid。

这些是 JSON 格式的数组

{

   "response": {

      "players": [

         {

            "steamid": "76561197989628470",

            "communityvisibilitystate": 3,

            "profilestate": 1,

            "personaname": "Archey",

            "lastlogoff": 1334719151,

            "commentpermission": 1,

            "profileurl": "http://steamcommunity.com/id/Archey6/",

            "avatar": "http://media.steampowered.com/steamcommunity/public/images/avatars/74/745b633a08937a5cf52bb44c2bdd3552f85455d7.jpg",

            "avatarmedium": "http://media.steampowered.com/steamcommunity/public/images/avatars/74/745b633a08937a5cf52bb44c2bdd3552f85455d7_medium.jpg",

            "avatarfull": "http://media.steampowered.com/steamcommunity/public/images/avatars/74/745b633a08937a5cf52bb44c2bdd3552f85455d7_full.jpg",

            "personastate": 1,

            "primaryclanid": "103582791432066081",

            "timecreated": 1177637717,

            "loccountrycode": "CA",

            "locstatecode": "SK"

         }

      ]

   }
4

1 回答 1

3

你为什么要解码然后编码json?

<?php
    $id = $_GET['id'];
    $key = 'xxx';

    $link = file_get_contents('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=' . $key . '&steamids=' . $id . '&format=json');
    $myarray = json_decode($link, true);

    print $myarray['response']['players'][0]['steamid'];
?>

或者如果你真的需要再次编码:

<?php
    $id = $_GET['id'];
    $key = 'xxx';

    $link = file_get_contents('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=' . $key . '&steamids=' . $id . '&format=json');
    $profile_info = json_decode($link);

    $json_response = json_encode($profile_info->response->players);
    $decoded = json_decode($json_response, true);
    print $json_response['steamid'];
?>
于 2012-04-19T03:11:31.557 回答