我需要从 JSON 中获取密钥字符串

我在 Laravel API 请求中包含以下 JSON:


{

  "questionary": {


    "directLeader": {

      "answer": "ALWAYS",

      "comments": "asdf"

    }

  },

  "id": 14 

}

我需要获取字符串“directLeader”,因为该键在请求中发生变化,我将其用作查询更新的参考。


一只斗牛犬
浏览 67回答 2
2回答

慕标5832272

你需要json_decode()像这样的 json$json = '{  "questionary": {    "directLeader": {      "answer": "ALWAYS",      "comments": "asdf"    }  },  "id": 14 }';$encoded_json = json_decode($json, true);dd($encoded_json['questionary']['directLeader']);请注意,true in 会将json_decode()对象转换为数组,没有true它则会将对象转换为对象

幕布斯7119047

将 json 转换为数组:$array = json_decode($json, true);要获取关联数组中的第一个键:$firstKey = array_key_first($array['问题']);$firstKey将包含您的动态密钥。
打开App,查看更多内容
随时随地看视频慕课网APP