首页 > 解决方案 > JSON 响应字符串以“null”结尾

问题描述

在 Postman 和 jQuery 中,我都收到了表单中的回复

{"key1": "value1", "key2": "value2"}null

尾随的 null 与任何试图在客户端解析它的东西混在一起,我不知道它来自哪里。如果我在 ingerror_log之前编码了 JSON echo,则没有尾随的 null,所以我假设它是一个字符串终止符,但我不认为 PHP 使用了以 null 结尾的字符串。如何摆脱这些空值?

被编码和返回的对象:

public function jsonSerialize()
{
    return [
        'internal_id' => $this->internal_id, //int
        'friendly_name' => $this->friendly_name, //string
        'external_id' => $this->external_id, //string
        'picture' => $this->picture //string
    ];
}

实际的 return 语句只是echo(json_encode($retval));

标签: phpjsonwordpressrestwordpress-plugin-creation

解决方案


一旦 PHP 文件执行完毕,您必须手动退出或返回而不是回显,否则它将NULL隐式返回并搞砸一切。学过的知识。


推荐阅读