php - Json解码成php
问题描述
我正在尝试将 API 解码为 PHP。
API EXM
{
"users":[
{"user":"john",
"user1":"Tabby",
"user2":"Ruby",
"user3":"Silver"}
]
}
倾斜用户 = 0(它是空的)
我尝试使用此代码,但它不起作用(空白页)
<?php
$json = '{ "users"[ {"user":"john", "user1":"Tabby", "user2":"Ruby", "user3":"Silver"}';
$arr = json_decode($json);
echo $arr->users->{0}->user; //blank page
echo $arr->users->user1; //blank page
echo $arr->users->0->user2; //syntax error, unexpected '0' (T_LNUMBER), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in
?>
解决方案
$arr->users
是一个数组而不是一个对象。因此,如果您将脚本更改为
<?php
$json = '{
"users":[
{"user":"john",
"user1":"Tabby",
"user2":"Ruby",
"user3":"Silver"}
]
}';
$arr = json_decode($json);
print_r($arr);
echo $arr->users[0]->user,"\n";
echo $arr->users[0]->user1,"\n";
echo $arr->users[0]->user2,"\n";
?>
...输出是...
stdClass Object
(
[users] => Array
(
[0] => stdClass Object
(
[user] => john
[user1] => Tabby
[user2] => Ruby
[user3] => Silver
)
)
)
john
Tabby
Ruby
推荐阅读
- ios - 使用 SwiftShield 时构建失败?
- tensorflow - 谷歌 Colab TensorFlow 1.15 GPU
- mysql - 如何使用 SELECT CONCAT() 在 MYSQL 中打印消息?
- python-3.x - 在 docker 容器中配置 Pyspark AWS 凭证
- python - 我设置了一个密钥,但不断收到错误“RuntimeError:使用 CSRF 需要密钥”。
- kubernetes - 创建错误:pod 被禁止:
配额失败: :必须指定limits.memory - javascript - ReferenceError:未定义进度(tesseract)
- apache-camel - Apache Camel 的培训和认证
- php - Shopware 6 中是否存在订单状态更改事件?
- git - 未跟踪和同时删除(git)