You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP解析接口返回JSON时无法获取acquired字段值的问题求助

错误原因

  • 初始定义的$result是JSON格式的字符串,不能直接通过数组下标读取字段,必须先调用json_decode()将整个字符串解析为数组才可操作
  • response下的item_json字段存储的是另一段被序列化的JSON数组字符串,需要再次调用json_decode()解析这段内容,才能拿到内部的acquired字段

正确实现代码

<?php

// curl请求返回的样例数据
$result = '{"response":{"item_json":"[{ \"accountid\":\"123\",\"itemid\":\"456\",\"quantity\":1,\"originalitemid\":\"789\",\"itemdefid\":\"100\",\"appid\":101112,\"acquired\":\"20210921T175213Z\",\"state\":\"\",\"origin\":\"external\",\"state_changed_timestamp\":\"20210921T175213Z\" }]"}}';

// 第一步:解析最外层的JSON字符串为数组
$content = json_decode($result, true);
// 第二步:解析item_json中嵌套的JSON数组字符串
$itemList = json_decode($content['response']['item_json'], true);
// 第三步:读取数组第一个元素的acquired字段赋值
$acquired = $itemList[0]['acquired'];

var_dump($acquired);
?>

运行上述代码后会输出:string(17) "20210921T175213Z",即为你需要提取的acquired字段值。

内容的提问来源于stack exchange,提问作者emily33

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 12:18:00