2017-07-06 135 views
0

您好我是与编码/ JSON golang工作,并返回与空json的错误:Golang编码/ 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) 
} 

有人可以帮助我。

+0

查看引用的JSON输出,它看起来像错误正在编码正确,但在JSON之后输出* null,表示单独的写入操作。是否有可能其他代码(也许这个函数的调用者)正在写这个输出?另外请注意,您接受状态码但不使用它,您始终使用StatusOK;并且你忽略了'Encode'返回的错误。 – Adrian

+0

函数我在另一个函数中使用,但编码返回为空的错误,我不知道编码/ json是否有问题,因为与另一个结构它工作正常。 – llCastro

+0

编码/ json没有问题。调用'EncodeErrorResponse'的函数在调用它之后,正在向'ResponseWriter'写入额外的输出。 – Adrian

回答

0

看着引用的JSON输出,它看起来像错误正在编码正确,但在JSON之后输出null,表示单独的写入操作。调用EncodeErrorResponse的函数在调用它之后正在将其他输出写入ResponseWriter。