Jsonapi解码
Json api decoding
这是我的 json
{
"rgInventory": {
"5455029633": {
"id":"5455029633",
"classid":"310776543",
"instanceid":"302028390",
"amount":"1"
}
}
}
这是我在 php
中解析 json 的方法
$content = file_get_contents("http://steamcommunity.com/profiles/76561198201055225/inventory/json/730/2");
$decode = json_decode($content);
foreach($decode->rgInventory->5455029633 as $appid){
echo $appid->id;
}
我需要获取 5455029633,但它在 foreach 中不起作用。
我也想把它存储在变量中。
Json,您提供的无效。从 "amount":"1",
中删除最后一个逗号,并且您缺少右大括号。然后你应该能够访问所需的值 $decode->rgInventory->{"5455029633"}
.
或者让您的生活更简单 ;) 并直接使用 assoc 数组 $decode = json_decode($content, true);
您需要将 true 作为第二个参数传递给函数 json_decode 以获取数组而不是对象:
PHP
<?php
$content = file_get_contents("http://steamcommunity.com/profiles/76561198201055225/inventory/json/730/2");
$decode = json_decode($content, true);
echo $decode['rgInventory']['6255037137']['id']; // will output the property 'id' of the inventory 6255037137
$invetory = $decode['rgInventory']['6255037137'] // will store the inventory 6255037137 in the variable $inventory
?>
这是我的 json
{
"rgInventory": {
"5455029633": {
"id":"5455029633",
"classid":"310776543",
"instanceid":"302028390",
"amount":"1"
}
}
}
这是我在 php
中解析 json 的方法$content = file_get_contents("http://steamcommunity.com/profiles/76561198201055225/inventory/json/730/2");
$decode = json_decode($content);
foreach($decode->rgInventory->5455029633 as $appid){
echo $appid->id;
}
我需要获取 5455029633,但它在 foreach 中不起作用。
我也想把它存储在变量中。
Json,您提供的无效。从 "amount":"1",
中删除最后一个逗号,并且您缺少右大括号。然后你应该能够访问所需的值 $decode->rgInventory->{"5455029633"}
.
或者让您的生活更简单 ;) 并直接使用 assoc 数组 $decode = json_decode($content, true);
您需要将 true 作为第二个参数传递给函数 json_decode 以获取数组而不是对象:
PHP
<?php
$content = file_get_contents("http://steamcommunity.com/profiles/76561198201055225/inventory/json/730/2");
$decode = json_decode($content, true);
echo $decode['rgInventory']['6255037137']['id']; // will output the property 'id' of the inventory 6255037137
$invetory = $decode['rgInventory']['6255037137'] // will store the inventory 6255037137 in the variable $inventory
?>