arrays - Go中的JSON解码有符号整数数组

标签 arrays json go struct slice

我正在尝试将 JSON 从整数数组解码为 Go 中的 byte slice 。

问题是只有当 JSON 中的数组包含正整数时我们才能这样做,因为它将在 Go 中被识别为 uint8。当数组包含负整数时它不起作用。

例如:

  • 这个数组可以工作:[1, 2, 3, 4, 5]
  • 此数组将不起作用:[-14, 2, 3, 4, 5](-14 为负数)

  • 这是我收到的错误消息
    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/

    相关文章:

    go - 语法错误 : unexpected name, 需要分号或换行符或 }

    javascript - 如何在 c3js 图表中显示 php 动态数组数据?

    javascript - 如何减去对象值

    json - 不处理从这里抛出的 Swift 错误

    javascript - 通过索引调用Json?

    go - 使用 Openshift/Client-go 进行 Openshift API 查询

    go - 无法获得具有相同键名(* http.Header)的响应头

    arrays - 通过代码设置二维数组的值

    c# - 将数组中的所有元素初始化为 NaN 的最快方法是什么?

    android - 如何解析JSON对象中的多个JSON对象?