如何从json数据中读取steamid

时间:2014-07-05 11:31:09

标签: php json

我对这个数组有问题。我不知道怎么读这个。

{
    "response": {
        "players": [
            {
                "steamid": "76561198132038395",
                "communityvisibilitystate": 3,
                "profilestate": 1,
                "personaname": "Spyer21",
                "lastlogoff": 1404500224,
                "commentpermission": 1,
                "profileurl": "http://steamcommunity.com/id/spyer21/",
                "avatar": "http://media.steampowered.com/steamcommunity/public/images/avatars/6c/6c231c01de768b1681a7888e36416b07c38746af.jpg",
                "avatarmedium": "http://media.steampowered.com/steamcommunity/public/images/avatars/6c/6c231c01de768b1681a7888e36416b07c38746af_medium.jpg",
                "avatarfull": "http://media.steampowered.com/steamcommunity/public/images/avatars/6c/6c231c01de768b1681a7888e36416b07c38746af_full.jpg",
                "personastate": 1,
                "primaryclanid": "103582791435750748",
                "timecreated": 1396613068,
                "personastateflags": 0,
                "loccountrycode": "CZ",
                "locstatecode": "52",
                "loccityid": 12260
            }
        ]

    }
}

请如何阅读必要的steamidThis is link to json response

3 个答案:

答案 0 :(得分:1)

此数据采用JSON格式

使用以下代码将其转换为数组:

$arr = json_decode($your_data, TRUE);

得到steamid如下:

echo $arr['response']['players'][0]['steamid']; // 76561198132038395

答案 1 :(得分:0)

这是一个json数组

使用json_decode函数

将其转换为数组

答案 2 :(得分:0)

这是javascript对象表示法。它主要用于javascript通过函数和页面传递数据,但php也使用它,即使代码中没有javascript。 格式可以转换为数组,反之亦然。

这两个函数允许您将json转换为数组,将数组转换为json字符串。 他们是

  

json_encode

     

json_decode

json_decode的第二个参数允许您获取关联数组,因此您只需使用要访问的位置名称即可访问。 好吧,如果你做一个数组的var_dump,你会发现键“steamid”包含在2个关联数组中,而一个无关联数组,第一个和最后一个键是0。 让我们访问它。

// the json string we want to process
$json = '
{
    "response": {
        "players": [
            {
                "steamid": "76561198132038395",
                "communityvisibilitystate": 3,
                "profilestate": 1,
                "personaname": "Spyer21",
                "lastlogoff": 1404500224,
                "commentpermission": 1,
                "profileurl": "http://steamcommunity.com/id/spyer21/",
                "avatar": "http://media.steampowered.com/steamcommunity/public/images/avatars/6c/6c231c01de768b1681a7888e36416b07c38746af.jpg",
                "avatarmedium": "http://media.steampowered.com/steamcommunity/public/images/avatars/6c/6c231c01de768b1681a7888e36416b07c38746af_medium.jpg",
                "avatarfull": "http://media.steampowered.com/steamcommunity/public/images/avatars/6c/6c231c01de768b1681a7888e36416b07c38746af_full.jpg",
                "personastate": 1,
                "primaryclanid": "103582791435750748",
                "timecreated": 1396613068,
                "personastateflags": 0,
                "loccountrycode": "CZ",
                "locstatecode": "52",
                "loccityid": 12260
            }
        ]

    }
}
';

// create an associative array (second parameter to true) from the json encoded
$array = json_decode($json, true);
// the array at the first glance
var_dump($array);
// now we have to access that array: the element we need is in position: "response", "players", key 0 (no-associative array) and "steamid"
$steam_id = $array["response"]["players"][0]["steamid"];
// let print to monitor what it contains
echo $steam_id;

现在,如果您需要使用其他内容更改该值,并且需要再次将其编码为json字符串,则必须调用上面列出的json_encode函数。

// convert it back again
$new_json = json_encode($array);
// just watch out the result!
var_dump($new_json);