php - 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));
解决方案
一旦 PHP 文件执行完毕,您必须手动退出或返回而不是回显,否则它将NULL
隐式返回并搞砸一切。学过的知识。
推荐阅读
- java - 是否可以为 Java 11 和 java 模块生成休眠元模型?
- c++ - C 样式字符串数组首先有效,然后不可读
- list - 如何在修改列表时迭代 Xamarin.Forms.Maps Map.Pins 列表?
- php - PHP 7.3 遇到非数字值
- python - 在 tensorflow.keras 中使用 BinaryCrossentropy(from_logits=True) 时应该使用什么作为目标向量
- tensorflow - 远程 Tensorboard 不适用于 SSH 隧道
- julia - 使自定义类型不可广播
- java - 模拟类型必须是:URL,但是是:URL$$PowerMock0
- firebase - 如何获取 QR 码的字符串值并将其存储到 firebase 以便链接到特定用户
- php - 正则表达式 - 转义方括号和边界