json - Golang 编码/json 编码添加 null

标签 json go encoding

您好,我正在使用 golang 处理 encoding/json,它返回带有 null 的 json 错误:

{"user_message":"Can't find any Query with those parameters","application_context":"GroupsRepository.GetGroupsByQuery: ApplicationError: UserMessage - Error querying database for many Groups. ApplicationContext - Groups.GetMany: pq: column \"refill_too_soon_gpi_digits\" does not exist"} null

以及我整理它的方式:

func EncodeErrorResponse(w http.ResponseWriter, err error, status int) {
    w.WriteHeader(http.StatusOK)
    w.Header().Set("Content-Type", "application/json")
    json.NewEncoder(w).Encode(err)
}

有人可以帮帮我吗?

最佳答案

看你引用的JSON输出,看起来错误编码是正确的,但是在JSON之后输出的是null,表示一个单独的写操作。调用 EncodeErrorResponse 的函数在调用它之后,正在将额外的输出写入 ResponseWriter。

关于json - Golang 编码/json 编码添加 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44959143/

相关文章:

javascript - 将 JSON 数据数组加一

go - 在 Go 中封装结构

java - Php 编码 - Java 解码(URL 部分)

encoding - TFS - 更改编码有什么作用?

maven - 过滤不同编码的资源

c# - Xamarin android数据保存到json文件

javascript - 我的 JSON feed 有什么问题?

javascript - json响应,读取数据

mongodb - 在 mongodb-go-driver 中通过子字符串正则表达式查询查找条目

amazon-web-services - 为 Amazon Aurora 启用数据 API