草庐IT

marshall

全部标签

json - Marshal 和 Unmarshal 一次。时间,得到不同的值

我正在尝试将我的Go代码从1.8更新到1.9。但是,我遇到了一些问题:packagesimple_structimport("time""encoding/json""github.com/stretchr/testify/assert""testing")typeSimpleStructstruct{CreateTimetime.TimeOtherstring}funcNewSimpleStruct()*SimpleStruct{return&SimpleStruct{CreateTime:time.Now(),}}funcTest(t*testing.T){s1:=NewSimpl

json - Marshal 和 Unmarshal 一次。时间,得到不同的值

我正在尝试将我的Go代码从1.8更新到1.9。但是,我遇到了一些问题:packagesimple_structimport("time""encoding/json""github.com/stretchr/testify/assert""testing")typeSimpleStructstruct{CreateTimetime.TimeOtherstring}funcNewSimpleStruct()*SimpleStruct{return&SimpleStruct{CreateTime:time.Now(),}}funcTest(t*testing.T){s1:=NewSimpl

json - Golang json.Marshal() 忽略 ByteArray 字段中的 omitempty

请看下面。https://play.golang.org/p/HXrqLqYIgz我的期望值是:{"Byte2":0,"Name":"bob"}但实际:{"ByteArray":[0,0,0,0],"Byte2":0,"Name":"bob"}根据文档(https://golang.org/pkg/encoding/json/)Theemptyvaluesarefalse,0,anynilpointerorinterfacevalue,andanyarray,slice,map,orstringoflengthzero.因此,json.Marshall()会忽略omitempty-ta

json - Golang json.Marshal() 忽略 ByteArray 字段中的 omitempty

请看下面。https://play.golang.org/p/HXrqLqYIgz我的期望值是:{"Byte2":0,"Name":"bob"}但实际:{"ByteArray":[0,0,0,0],"Byte2":0,"Name":"bob"}根据文档(https://golang.org/pkg/encoding/json/)Theemptyvaluesarefalse,0,anynilpointerorinterfacevalue,andanyarray,slice,map,orstringoflengthzero.因此,json.Marshall()会忽略omitempty-ta

json - 停止 json.Marshal() 从 float 中去除尾随零

我遇到了以下问题:我的golang程序将一些信息转换为JSON。例如,它会产生以下json:{"value":40,"unit":"some_string"}问题是值的“输入”是40.0,并且编码会去除尾随零。如果读取JSON的EPL能够在没有.0的情况下将40读取为float,那就没问题了因此JSON输出应该如下所示:{"value":40.0,"unit":"some_string"}是否有可能“阻止”json.Marshal()删除零?编辑:值必须是一个float 最佳答案 @icza提供了一个很好的答案,但只是为了提供另一种

json - 停止 json.Marshal() 从 float 中去除尾随零

我遇到了以下问题:我的golang程序将一些信息转换为JSON。例如,它会产生以下json:{"value":40,"unit":"some_string"}问题是值的“输入”是40.0,并且编码会去除尾随零。如果读取JSON的EPL能够在没有.0的情况下将40读取为float,那就没问题了因此JSON输出应该如下所示:{"value":40.0,"unit":"some_string"}是否有可能“阻止”json.Marshal()删除零?编辑:值必须是一个float 最佳答案 @icza提供了一个很好的答案,但只是为了提供另一种

json - Golang Json.Marshal 错误

我一直在尝试将map编码为JSON,但到目前为止我一直没有成功。Json.Marshal不是编码值,它只是编码键。https://gist.github.com/rahulpache/9174490packagemainimport("encoding/json""fmt")typenodestruct{valuestringexpiryfloat64settimefloat64}funcmain(){varx=make(map[string]node)x["hello"]=node{value:"world",expiry:1,settime:2}x["foo"]=node{valu

json - Golang Json.Marshal 错误

我一直在尝试将map编码为JSON,但到目前为止我一直没有成功。Json.Marshal不是编码值,它只是编码键。https://gist.github.com/rahulpache/9174490packagemainimport("encoding/json""fmt")typenodestruct{valuestringexpiryfloat64settimefloat64}funcmain(){varx=make(map[string]node)x["hello"]=node{value:"world",expiry:1,settime:2}x["foo"]=node{valu

go - 如何有选择地编码(marshal)结构的 JSON?

我有一个结构:typePaperstruct{PIDint`json:"pID"`PTitlestring`json:"pTitle"`PDescstring`json:"pDesc"`PPwdstring`json:"pPwd"`}大多数情况下,我会将整个结构编码为JSON。然而,偶尔,我需要结构的简短版本;即编码一些属性,我应该如何实现这个功能?typeBriefPaperstruct{PIDint`json:"-"`//notneededPTitlestring`json:"pTitle"`PDescstring`json:"pDesc"`PPwdstring`json:"-"`

go - 如何有选择地编码(marshal)结构的 JSON?

我有一个结构:typePaperstruct{PIDint`json:"pID"`PTitlestring`json:"pTitle"`PDescstring`json:"pDesc"`PPwdstring`json:"pPwd"`}大多数情况下,我会将整个结构编码为JSON。然而,偶尔,我需要结构的简短版本;即编码一些属性,我应该如何实现这个功能?typeBriefPaperstruct{PIDint`json:"-"`//notneededPTitlestring`json:"pTitle"`PDescstring`json:"pDesc"`PPwdstring`json:"-"`