【问题标题】:Remove model name from json results in cakephp 2.x从 json 中删除模型名称会导致 cakephp 2.x
【发布时间】:2016-10-14 20:15:01
【问题描述】:

我正在使用 cakephp 2.x 并使用以下内容返回一些 json 数据;我想从结果中删除模型名称 User 以便只返回键和值。

$this->set('_serialize', 'json');

并且我的结果以如下格式返回;

[
{
    "User": {
        "fullname": "John Smith"
    }
},
{
    "User": {
        "fullname": "Jiles Smith"
    }
}
}
]

我尝试了以下方法;

$json = json_encode(Set::extract('/User/.', $json));

但它的返回就像;

"[{\"fullname\":\"Ashley Smith\"},{\"fullname\":\"Leigh Smith\"},{\"fullname\":\"Charlie Chocolate\"}]"

【问题讨论】:

  • 发布预期输出 ...
  • 你好像在使用 JSON 视图,所以去掉json_encode 调用,它是多余的......
  • 感谢@ndm,解决了问题:)

标签: php cakephp


【解决方案1】:

@ndm 在上面发布了解决方案。我会将他的答案标记为正确,但我不确定如何做。

解决办法是替换

$json = json_encode(Set::extract('/User/.', $json); 

$json = Set::extract('/User/.', $json);

【讨论】:

  • 不能接受评论,只能接受实际答案,所以请接受自己的答案(可能在 48 小时后),完全可以。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-15
  • 1970-01-01
  • 2020-01-23
  • 2013-10-08
  • 2017-03-03
  • 2013-02-16
  • 1970-01-01
相关资源
最近更新 更多