我正在尝试将 JSON 从整数数组解码为 Go 中的 byte slice 。
问题是只有当 JSON 中的数组包含正整数时我们才能这样做,因为它将在 Go 中被识别为 uint8。当数组包含负整数时它不起作用。
例如:
这是我收到的错误消息
Cannot unmarshal config file; err= json: cannot unmarshal number -14 int to Go struct field <struct_field> of type uint8
有什么方法可以将带有负整数的 JSON 解码数组转换为 Go 中的字节 slice ?
最佳答案
Is there any way I can do JSON unmarshal array with negative integers into a byte slice in Go?
不,因为 negative numbers are outside of the valid range of byte values ,就像任何大于 255 的数字一样。
关于arrays - Go中的JSON解码有符号整数数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61362980/