草庐IT

UnMarshal

全部标签

go - 在运行时确定结构时进行 JSON Unmarshal

通常我们在Go中解码一个json对象:我是Go的菜鸟,所以如果下面的某些语法似乎不正确,请原谅我。typeItemstruct{Quantityint`json:"quantity,omitempty"`Pricefloat64`json:"price,omitempty"`Currencystring`json:"currency,omitempty"`}output:=&Item{}err:=json.Unmarshal([]byte(data),output)现在问题是我的json在运行时可能会因某些字段而异。价格可以是字符串、具有不同值的数组或在一个对象中包含货币和价格的jso

go - 在运行时确定结构时进行 JSON Unmarshal

通常我们在Go中解码一个json对象:我是Go的菜鸟,所以如果下面的某些语法似乎不正确,请原谅我。typeItemstruct{Quantityint`json:"quantity,omitempty"`Pricefloat64`json:"price,omitempty"`Currencystring`json:"currency,omitempty"`}output:=&Item{}err:=json.Unmarshal([]byte(data),output)现在问题是我的json在运行时可能会因某些字段而异。价格可以是字符串、具有不同值的数组或在一个对象中包含货币和价格的jso

json - 是否可以动态设置 json.Unmarshal 的输出?

我有以下Go代码:vartypeRegistry=make(map[string]reflect.Type)funcinit(){typeRegistry["User"]=reflect.TypeOf(User{})}funcmakeInstance(namestring)interface{}{v:=reflect.New(typeRegistry[name]).Elem()returnv.Interface()}funcInvoke(anyinterface{},namestring,body[]byte,signatureSignature){args:=signature.Ar

json - 是否可以动态设置 json.Unmarshal 的输出?

我有以下Go代码:vartypeRegistry=make(map[string]reflect.Type)funcinit(){typeRegistry["User"]=reflect.TypeOf(User{})}funcmakeInstance(namestring)interface{}{v:=reflect.New(typeRegistry[name]).Elem()returnv.Interface()}funcInvoke(anyinterface{},namestring,body[]byte,signatureSignature){args:=signature.Ar

golang XML : unmarshal ignores namespace

我正在我们的Go系统中实现一项服务,该服务从外部SOAP服务读取数据。现在我正在为它编写测试,我遇到了这个问题:unabletounmarshalrequestbodyfortesting:expectedelementtypebuthave如果我转储我的数据,我有这个:所以我在XML中确实有正确的命名空间。作为记录,这是我试图将数据解码到的结构:typecollectRequestXMLstruct{XMLNamexml.Name`xml:"soapenv:Envelope"`SoapEnvstring`xml:"xmlns:soapenv,attr"`XMLNsstring`xml

golang XML : unmarshal ignores namespace

我正在我们的Go系统中实现一项服务,该服务从外部SOAP服务读取数据。现在我正在为它编写测试,我遇到了这个问题:unabletounmarshalrequestbodyfortesting:expectedelementtypebuthave如果我转储我的数据,我有这个:所以我在XML中确实有正确的命名空间。作为记录,这是我试图将数据解码到的结构:typecollectRequestXMLstruct{XMLNamexml.Name`xml:"soapenv:Envelope"`SoapEnvstring`xml:"xmlns:soapenv,attr"`XMLNsstring`xml

go - 为什么在有 Unmarshal 实现时不解码嵌入式结构?

我有一个ServiceAccount类型,它嵌入了另外两种类型(用户和策略)。然而,由于Policy类型的Unmarshaler实现,似乎完全忽略了User类型的字段。这种行为有什么充分的理由吗?对我来说这看起来像是一个错误,因为json包可以通过反射看到我们嵌入了两种类型,而不仅仅是Policy类型。我知道我也可以通过在ServiceAccount类型上实现Unmarshaler接口(interface)来“解决”这个问题。packagemainimport("encoding/json""fmt")typeServiceAccountstruct{UserPolicy}typeUs

go - 为什么在有 Unmarshal 实现时不解码嵌入式结构?

我有一个ServiceAccount类型,它嵌入了另外两种类型(用户和策略)。然而,由于Policy类型的Unmarshaler实现,似乎完全忽略了User类型的字段。这种行为有什么充分的理由吗?对我来说这看起来像是一个错误,因为json包可以通过反射看到我们嵌入了两种类型,而不仅仅是Policy类型。我知道我也可以通过在ServiceAccount类型上实现Unmarshaler接口(interface)来“解决”这个问题。packagemainimport("encoding/json""fmt")typeServiceAccountstruct{UserPolicy}typeUs

json - Golang JSON Unmarshal 序列号

我有一个关于json.Unmarshal的问题。Playground示例https://play.golang.org/p/3d0nUv7PSJ问题是:JSON不会将“1”解码为float。我的意思是“1”是一个序列化的JSON,它应该是一个数字。我做错了什么吗?:D 最佳答案 在JSON中,"1"是一个字符串。如果您在示例中使用1,则它会被正确地解码为float。Playground:https://play.golang.org/p/60symMsRSW 关于json-GolangJ

json - Golang JSON Unmarshal 序列号

我有一个关于json.Unmarshal的问题。Playground示例https://play.golang.org/p/3d0nUv7PSJ问题是:JSON不会将“1”解码为float。我的意思是“1”是一个序列化的JSON,它应该是一个数字。我做错了什么吗?:D 最佳答案 在JSON中,"1"是一个字符串。如果您在示例中使用1,则它会被正确地解码为float。Playground:https://play.golang.org/p/60symMsRSW 关于json-GolangJ