获取JSON的第一个条目
Obtain the first entry of JSON
我在 URL 中有这个 JSON:
{"success":true,"rgInventory":{"6073259621":{"id":"6073259621","classid":"1660549198","instanceid":"188530139","amount":"1","pos":1}}}
我需要获取 rgInventory
之后的第一个条目。问题是问题是假设我不知道有"6073259621"
。我不知道那里有什么如何获得它?
我尝试了这个但不起作用:
$obj = json_decode(file_get_contents($url), true);
$obj2 = json_decode(json_encode($obj['rgInventory']), true);
$obj3 = json_decode(json_encode($obj2), true);
echo $obj3;
如果 JSON 字符串有效且如下所示
{ "success":true,
"rgInventory":{
"6073259621":{
"id":"6073259621",
"classid":"1660549198",
"instanceid":"188530139",
"amount":"1",
"pos":1
}
}
}
get the decode in $obj like
$obj = json_decode(file_get_contents($url), true);
then your first entry would be
echo array_keys($obj['rgInventory'])[0];
to understand it clearly and to know where is "6073259621"
$obj = json_decode(file_get_contents($url), true);
var_dump($obj);
also note the difference
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
var_dump(json_decode($json, true));
输出 将是..
// decode as object
object(stdClass)#1 (5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}
// decode as array
array(5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}
在 JSON 解码后:
$obj = json_decode(file_get_contents($url), true);
您可以使用 reset
.
从 rgInventory
中获取第一项,而不管它的键是什么。
$first_entry = reset($obj['rgInventory']);
这是使用 each() 获取键和值(数组)的简单方法:
$data = json_decode(file_get_contents($url), true);
list($key, $val) = each($data['rgInventory']);
echo $key;
print_r($val);
产量:
6073259621
Array
(
[id] => 6073259621
[classid] => 1660549198
[instanceid] => 188530139
[amount] => 1
[pos] => 1
)
但我只是注意到 id
与密钥相同,所以并不需要。
我在 URL 中有这个 JSON:
{"success":true,"rgInventory":{"6073259621":{"id":"6073259621","classid":"1660549198","instanceid":"188530139","amount":"1","pos":1}}}
我需要获取 rgInventory
之后的第一个条目。问题是问题是假设我不知道有"6073259621"
。我不知道那里有什么如何获得它?
我尝试了这个但不起作用:
$obj = json_decode(file_get_contents($url), true);
$obj2 = json_decode(json_encode($obj['rgInventory']), true);
$obj3 = json_decode(json_encode($obj2), true);
echo $obj3;
如果 JSON 字符串有效且如下所示
{ "success":true,
"rgInventory":{
"6073259621":{
"id":"6073259621",
"classid":"1660549198",
"instanceid":"188530139",
"amount":"1",
"pos":1
}
}
}
get the decode in $obj like
$obj = json_decode(file_get_contents($url), true);
then your first entry would be
echo array_keys($obj['rgInventory'])[0];
to understand it clearly and to know where is "6073259621"
$obj = json_decode(file_get_contents($url), true);
var_dump($obj);
also note the difference
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
var_dump(json_decode($json, true));
输出 将是..
// decode as object
object(stdClass)#1 (5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}
// decode as array
array(5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}
在 JSON 解码后:
$obj = json_decode(file_get_contents($url), true);
您可以使用 reset
.
rgInventory
中获取第一项,而不管它的键是什么。
$first_entry = reset($obj['rgInventory']);
这是使用 each() 获取键和值(数组)的简单方法:
$data = json_decode(file_get_contents($url), true);
list($key, $val) = each($data['rgInventory']);
echo $key;
print_r($val);
产量:
6073259621
Array
(
[id] => 6073259621
[classid] => 1660549198
[instanceid] => 188530139
[amount] => 1
[pos] => 1
)
但我只是注意到 id
与密钥相同,所以并不需要。