gpt4 book ai didi

arrays - 如何将序列化的 JSON View 数据输出为对象数组,而不是包装在外部对象中?

转载 作者:行者123 更新时间:2023-12-02 09:43:42 25 4
gpt4 key购买 nike

我正在将 CakePHP 数组的返回值转换为 JSON,目前是这样的:

{
"platformusers" : [
{
"id" : "1",
"name" : "user1"
},
{
"id" : "3",
"name" : "user3"
}
]
}

我希望它是这样的:

[
{
"id" : "1",
"name" : "user1"
},
{
"id" : "3",
"name" : "user3"
}
]

我正在尝试使用 Set::extract('{n}.Model', $data) Hash::extract('{n}.Model', $data) 一点运气都没有。

完整代码:

    $platformusers = $this->Platformuser->find('all', array(
'fields' => array('Platformuser.id', 'Platformuser.name')
));

$platformusers = Hash::extract('{n}.Platformuser', $platformusers);

$this->set(array(
'platformusers' => $platformusers,
'_serialize' => array('platformusers')
));

最佳答案

_serialize选项设置一个字符串而不是数组。数组表示可能有多个 View 变量需要序列化,这需要将它们打包到单独的对象属性中。

$this->set(array(
'platformusers' => $platformusers,
'_serialize' => 'platformusers'
));

这应该会给你想要的结果。

关于arrays - 如何将序列化的 JSON View 数据输出为对象数组,而不是包装在外部对象中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19250477/

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