我正在将 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/