如何将序列化的JSON视图数据输出为对象数组,而不是包装在外部对象中? [英] How to output serialized JSON view data as an array of objects, instead of wrapped in an outer object?
问题描述
{
)将CakePHP数组的返回值转换为JSON,平台用户:[
{
id:1,
name:user1
},
{
id :3,
name:user3
}
]
}
我希望它是这样的:
[
{
id:1,
name:user1
},
{
id:3,
名称:user3
}
]
Hash :: extract('{n} .Model',$ data)根本没有运气
完整代码:
$ platformusers = $ this-> ;平台用户 - > find('all',array(
'fields'=> array('Platformuser.id','Platformuser.name')
));
$ platformusers = Hash :: extract('{n} .Platformuser',$ platformusers);
$ this-> set(array(
'platformusers'=> $ platformusers,
'_serialize'=> array('platformusers')
));
code>用于 _serialize
选项而不是数组<$ code>。一个
数组
表示可能有多个视图变量需要被序列化,并且需要将它们打包到单独的对象属性中。
$ (
'platformusers'=> $ platformusers,
'_serialize'=>'platformusers'
$ this->
));
这会给你想要的结果。
I am converting the returning value of CakePHP array to JSON, currently its like this:
{
"platformusers" : [
{
"id" : "1",
"name" : "user1"
},
{
"id" : "3",
"name" : "user3"
}
]
}
And I want it to be like this:
[
{
"id" : "1",
"name" : "user1"
},
{
"id" : "3",
"name" : "user3"
}
]
I am trying with Set::extract('{n}.Model', $data) Hash::extract('{n}.Model', $data) with no luck at all.
Full code:
$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')
));
Set a string
for the _serialize
option instead of an array
. An array
indicates that there might be multiple view vars that need to be serialized, and that requires them to be packed into separate object properties.
$this->set(array(
'platformusers' => $platformusers,
'_serialize' => 'platformusers'
));
That should give you the desired result.
这篇关于如何将序列化的JSON视图数据输出为对象数组,而不是包装在外部对象中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!