小编kaz*_*uri的帖子

json在go中解析int64; 空值

我正在尝试解析Go中的json流.我创建了一个简化的例子:

 package main
 import (
    "encoding/json"
    "fmt"
 )

 var d = []byte(`{ "world":[{"data": 2251799813685312}, {"data": null}]}`)

 type jsonobj struct{ World []World }
 type World struct{ Data int64 }

 func main() {
    var data jsonobj
    jerr := json.Unmarshal(d, &data)
    fmt.Println(jerr)
 }
Run Code Online (Sandbox Code Playgroud)

这会给我

go run testmin.go
json: cannot unmarshal null into Go value of type int64
Run Code Online (Sandbox Code Playgroud)

我在sql包中找到了一个可以为空的int64 ,但是json似乎无法处理它.

是否存在json可以处理的可为空的int64类型?如果可能的话,我会很高兴将json null转换为-1或MinValue.

谢谢你的意见,Fabian

json go int64

7
推荐指数
1
解决办法
4026
查看次数

标签 统计

go ×1

int64 ×1

json ×1