【发布时间】:2020-11-20 17:39:40
【问题描述】:
我在请求 laravel API 中有这个 JSON:
{
"questionary": {
"directLeader": {
"answer": "ALWAYS",
"comments": "asdf"
}
},
"id": 14
}
我需要获取字符串“directLeader”,因为这个键在请求中发生了变化,我用它作为查询更新的参考。
【问题讨论】:
我在请求 laravel API 中有这个 JSON:
{
"questionary": {
"directLeader": {
"answer": "ALWAYS",
"comments": "asdf"
}
},
"id": 14
}
我需要获取字符串“directLeader”,因为这个键在请求中发生了变化,我用它作为查询更新的参考。
【问题讨论】:
你需要json_decode()你喜欢的json
$json = '{
"questionary": {
"directLeader": {
"answer": "ALWAYS",
"comments": "asdf"
}
},
"id": 14
}';
$encoded_json = json_decode($json, true);
dd($encoded_json['questionary']['directLeader']);
注意
json_decode() 中的 true 会将对象转换为数组,没有 true 它将是一个对象
【讨论】:
$array = json_decode($json, true);
$firstKey = array_key_first($array['questionary']);
$firstKey 将包含您的动态密钥。
【讨论】: