【发布时间】:2021-12-05 19:10:11
【问题描述】:
我想将此响应更改为数组
这是我通过 API 收到的值:"{'db_password': 'TgD31F#4124!'}"
我想删除引号,只保留TgD31F#4124!
代码示例
$db = Http::get(HERE_URL);
return $db->body(); // received it here "{'db_password': 'TgD31F#4124!'}"
【问题讨论】:
我想将此响应更改为数组
这是我通过 API 收到的值:"{'db_password': 'TgD31F#4124!'}"
我想删除引号,只保留TgD31F#4124!
代码示例
$db = Http::get(HERE_URL);
return $db->body(); // received it here "{'db_password': 'TgD31F#4124!'}"
【问题讨论】:
您应该在响应变量上使用json() 方法或object() 方法。 API Reference
json method: Get the JSON decoded body of the response as an array or scalar value.
object method: Get the JSON decoded body of the response as an array or scalar value.
用法:
$response = Http::get("www.api.com/...");
$decodedBody = $response->json();
或
$decodedBody = $response->object();
编辑: 所有可用的方法都列在the documentation中。
【讨论】:
$decodedBody['db_password'] 来访问您想要的字段值?看起来你做错了什么,因为这是有效的文档。
在 ajax 中添加数据类型:json
然后
response.db_password
否则,尝试将其解析为 json。
let RESULT = JSON.parse(response) console.log(RESULT.db_password)
【讨论】:
$result = $db->body()->toArray(); 或 $result = json_encode($db->body()); return $result->db_password;