【问题标题】:print JSON array - steam web api打印 JSON 数组 - steam web api
【发布时间】:2023-03-20 04:30:01
【问题描述】:

我一直在玩弄使用 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 生成器提供给我的密钥所取代。但是这段代码 sn-p 我只返回字符 { 它应该返回 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"

         }

      ]

   }

【问题讨论】:

    标签: json api web


    【解决方案1】:

    你为什么要解码然后编码 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'];
    ?>
    

    【讨论】:

    • 不太清楚我为什么对其进行编码,但是尝试您的第一个示例给了我错误Cannot use object of type stdClass as array
    • 在哪一行?我在我的计算机中复制了您的示例 json 文件并尝试了这个:&lt;?php $link = file_get_contents('your_example.json', true); $myarray = json_decode($link, true); print $myarray['response']['players'][0]['steamid']; ?&gt;。它有效。
    • 你还需要在 json 文件的末尾添加另一个大括号。
    • print($myarray['response']['players'][0]['steamid']); 你应该在这里添加一个零。抱歉弄错了。
    • 即使使用 [0] 它仍然会在第 8 行,即打印行给出相同的错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-24
    • 1970-01-01
    • 2020-04-16
    相关资源
    最近更新 更多