json解析int64中的go;空值 [英] json parsing of int64 in go; null values

查看:569
本文介绍了json解析int64中的go;空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Go中解析json流。我已经创建了一个简化的示例:

pre $ $ b $ import $ $ b $ encoding/ json
fmt


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

类型jsonobj struct {World [] World}
类型世界struct {Data int64}

func main(){
var data jsonobj
jerr:​​= json.Unmarshal(d,& data)
fmt.Println(jerr)
}

这会给我

  go run testmin.go 
json:不能将null解组成空格到类型为int64的Go值

我在 sql包,但json似乎无法处理它。

是否有可以处理的可空int64类型?如果可能的话,我会很满意将json null 翻译为-1或MinValue。



感谢您的输入,
Fabian

解决方案

只需使用 * int64 。指针可以是零,也可以指向具有关联值的int64,并且它们可以与Go的JSON包配合使用。


I'm trying to parse a json stream in Go. I've created a simplified example:

 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)
 }

this will give me

go run testmin.go
json: cannot unmarshal null into Go value of type int64

I've found a nullable int64 in the sql package, but json doesn't seem to be able to handle it.

Is there a nullable int64 type that json can handle? If possible I'd be happy with the json null being translated to, -1 or MinValue.

Thank you for your input, Fabian

解决方案

Just use a *int64. A pointer can either be nil or it can point to an int64 with an associated value and they work fine with Go's JSON package.

这篇关于json解析int64中的go;空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆