json “顶级值后的无效字符 '\x00'”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25187718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:28:39  来源:igfitidea点击:

"invalid character '\x00' after top-level value"

jsongo

提问by Oliver.Oakley

I am getting this error while unmarshalling json in a for loop. The first time through the loop is unmarshalling fine but on the next iteration I am getting this error.

在 for 循环中解组 json 时出现此错误。通过循环的第一次解组很好,但在下一次迭代中我收到此错误。

I am new to golang and this error message is not clear. Can someone please explain in what cases this error occurs and how I am supposed to avoid it.

我是 golang 的新手,这个错误信息不清楚。有人可以解释在什么情况下会发生此错误以及我应该如何避免它。

采纳答案by Oliver.Oakley

Thank you for answering my question.

谢谢你回答我的问题。

This error came due to incorrect json before that there is little more story to this,

这个错误是由于 json 不正确造成的,之前没有更多的故事,

I have a field detailof type json.RawMessage I have SELECT query where I am trying to read json from the table and scan to detailand appending it to the struct.

我有一个json.RawMessage 类型的字段详细信息我有 SELECT 查询,我试图从表中读取 json 并扫描到详细信息并将其附加到结构中。

After for loop ends when I look at detailJSON it got same record multiple times with and without end braces. After giving it so much thinking I figured casting detail with byte helped.

在 for 循环结束后,当我查看细节JSON 时,它多次获得相同的记录,无论是否有大括号。在考虑了这么多之后,我认为使用字节进行铸造细节有所帮助。

Eg: instead of just rows.Scan(&detail) try this: rows.Scan((*[]byte)(&detail)) solved the above problem

例如:而不仅仅是 rows.Scan(&detail) 试试这个:rows.Scan((*[]byte)(&detail)) 解决了上述问题

回答by VonC

Looking at the source code of encoding/json/scanner.go

查看源代码 encoding/json/scanner.go

// stateEndTop is the state after finishing the top-level value,
// such as after reading `{}` or `[1,2,3]`.
// Only space characters should be seen now.
func stateEndTop(s *scanner, c int) int {
    if c != ' ' && c != '\t' && c != '\r' && c != '\n' {
        // Complain about non-space byte on next call.
        s.error(c, "after top-level value")
    }
    return scanEnd
}

So check how your JSON string ends.

因此,请检查您的 JSON 字符串如何结束。

For example, in this thread, to illustrate a potential issue:

例如,在这个线程中,为了说明一个潜在的问题:

ReadFromUDPcan return a packet of any size, from 1 to 2000 bytes, you need to reslice your buffer using the number of bytes actually read.

ReadFromUDP可以返回任何大小的数据包,从 1 到 2000 字节,您需要使用实际读取的字节数重新切片缓冲区。

json.Unmarshal(buf[:n], &msg)

Same in this thread:

这个线程中相同:

request := make([]byte, 1024)
read_len, err := conn.Read(request)
request_right := request[:read_len]
j := new(Json)
err := j.UnmarshalJSON(request) // not working

if read_len < len(request)then request will contain extra "\x00" at the end and that's why UnmarshalJSON(request)won't work.

如果read_len < len(request)然后请求将\x00在末尾包含额外的“ ”,这就是为什么UnmarshalJSON(request)不起作用。