我正在尝试解析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)
}
这会给我
go run testmin.go
json: cannot unmarshal null into Go value of type int64
我在sql package中找到了一个可以为空的int64,但是json似乎无法处理它。
json可以处理一个可以为null的int64类型吗?如果可能的话,我会很高兴将json null 转换为-1或MinValue。
感谢您的投入, 费边
答案 0 :(得分:13)
只需使用*int64
即可。指针可以是nil,也可以指向带有关联值的int64,它们可以与Go的JSON包一起使用。
答案 1 :(得分:0)
https://github.com/guregu/null包含null.Int null.String等,以及相应的JSON序列化/反序列化。