【问题标题】:Phalcon 4 data strange symbol during getJsonRawBodygetJsonRawBody 期间的 Phalcon 4 数据奇怪符号
【发布时间】:2021-12-12 16:09:58
【问题描述】:

我正在使用Phalcon 4,并以 JSON 格式向 Phalcon 控制器发送 POST。

JSON 是:

birthday    "12/12/2020"
country "ddf"
id  "15bbde30-3714-11ec-95e7-8163123e4768"
name    "df"

但是当我分析它时:

public function post(): ResponseInterface
    {
        $this->view->disable();

     
        $body =  $this->request->getJsonRawBody();
        return $this->response->setStatusCode(502)->setContent(json_encode(($body-> birthday)));

结果是:

"12\/12\/2020"

我不明白为什么 Phalcon 会添加这个奇怪的符号 \/

如果我再做一次类似的测试(我已经删除了-> birthday):

public function post(): ResponseInterface
    {
        $this->view->disable();

     
        $body =  $this->request->getJsonRawBody();
        return $this->response->setStatusCode(502)->setContent(json_encode(($body)));

我有例外的结果:

birthday    "12/12/2020"
country "ddf"
id  "15bbde30-3714-11ec-95e7-8163123e4768"
name    "df"

你有没有办法避免它?

【问题讨论】:

    标签: phalcon


    【解决方案1】:

    您的问题不在于 Phalcon,而是使用 php 的 json_encode()

    如果您不想转义斜线,只需添加 JSON_UNESCAPED_SLASHES 标志

    return $this->response
        ->setStatusCode(502)
        ->setContent(json_encode($body, JSON_UNESCAPED_SLASHES));
    

    检查 json_encode() here 上的 php 文档

    【讨论】:

      猜你喜欢
      • 2014-07-12
      • 1970-01-01
      • 2013-08-16
      • 2013-12-16
      • 1970-01-01
      • 2013-01-24
      • 1970-01-01
      • 2020-05-25
      • 2011-01-17
      相关资源
      最近更新 更多