gpt4 book ai didi

PHP/JSON - 如何解析 stdClass 对象?

转载 作者:行者123 更新时间:2023-12-04 06:16:34 28 4
gpt4 key购买 nike

我有一些 JSON,我已经通过一些 PHP 运行它,基本上解析 JSON 并按如下方式对其进行解码:

$request = array(
'object' => 'App',
'action' => 'getList',
'args' => array(
'sort' => 1,
'appsPerPage' => 15,
'page' => 1,
'deviceid' => 1));


$wrapper = array(
'request' => json_encode($request));

$wrapper = urlencode(json_encode($wrapper));

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://api.apptrackr.org/');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "request=$wrapper");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

$response = json_decode($response);

结果:
    object(stdClass)#2 (3) { ["code"]=> int(200) ["data"]=> string(14100) "{"apps": {"id":303917165,"add_date":1314083737,"last_modification":1314083737,"name":"my Fish 3D Aquarium","seller":"

最后还有更多的“应用程序”......

现在,我试试这个:
    foreach ($response->apps as $app) {
.......
......
}

它不起作用...需要帮助。

谢谢

最佳答案

看起来您的后端方法返回类似

{code:200, data: '{"apps":{"id":303917165, ....}"};

注意数据是 json_encoded 字符串 .

解决此问题的简单方法是:
$response = json_decode($response);
$real_response = json_decode($response->data,true);
//$real_response is an array with 'apps' key containing your data

正确的方法是修复后端以仅返回数据,因此原始响应类似于
{"apps":{"id":303917165,....}}

关于PHP/JSON - 如何解析 stdClass 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7158369/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com