草庐IT

Unmarshal

全部标签

xml - Go XML Unmarshal 示例无法编译

godocs中的Xml示例被打破。有谁知道如何让它工作?当我编译它时,结果是:xmlexample.go:34:cannotuse"name"(typestring)astypexml.Nameinfieldvaluexmlexample.go:34:cannotusenilastypestringinfieldvaluexmlexample.go:34:toofewvaluesinstructinitializer相关代码如下:packagemainimport("bytes""xml")typeEmailstruct{Wherestring"attr";Addrstring;}ty

java - JAXB UnMarshal Collection 元素顺序

假设我有以下XML...这是我的代码:finalJAXBContextcontext=JAXBContext.newInstance(Names.class);finalUnmarshallerum=context.createUnmarshaller();finalInputStreamin=newFileInputStream(file);finalReaderreader=newInputStreamReader(in,Charset.forName("UTF-8"));finalNamesnamesList=(Names)um.unmarshal(reader);...现在我找

reflection - 不能在用反射制作的 slice 上使用范围,然后传递 json.Unmarshal

我从下面的代码中得到以下错误:invalidindirectoftypedSlice(typeinterface{})cannotrangeovertypedSlice(typeinterface{})这让我感到困惑,因为reflect.TypeOf(copy)匹配t的类型。funcUnmarshal(treflect.Type)[]interface{}{ret:=[]interface{}{}s:=`[{"Name":"Thequick..."}]`slice:=reflect.Zero(reflect.SliceOf(t))o:=reflect.New(slice.Type())

json - Go json.Unmarshal 返回 false 结构

这里是Golang新手我正在尝试从一个.json文件(与Go代码在同一目录中)解析为一个包含其他结构的结构,而我最接近成功的是一个包含bool值false的结构,这对我来说听起来很糟糕.这是到目前为止我的Go代码中的内容packagemainimport("encoding/json""fmt""io/ioutil")typeAppstruct{Namestring`json:"app:name"`}typeDatabasestruct{Typestring`json:"database:type"`Namestring`json:"database:name"`Userstring`

json - 具有导出和未导出字段的 Golang Marshal/Unmarshal JSON

我见过很多编码/取消编码只有未导出字段的结构的方法。但是我怎样才能对混合字段执行此操作?给定一个结构:typeTeststruct{fieldAstring`json:"fieldA"`FieldBint`json:"fieldB"`FieldCstring`json:"fieldC"`}如何编写MarshalJSON/UnmarshalJSON函数,以便将fieldA与FieldB和FieldC一起传输?以下编译,但在我运行时溢出调用堆栈。我的猜测是我正在递归编码对象,但我不确定在编码时如何保留导出和未导出的字段。func(t*Test)MarshalJSON()([]byte,er

JSON Unmarshal 无法按预期使用结构

这个问题在这里已经有了答案:Mystructuresarenotmarshallingintojson[duplicate](3个答案)关闭6年前。我有以下代码:packagemainimport"encoding/json"import"fmt"typeSuperNumstruct{numstring}funcmain(){byt:=[]byte(`{"num":"6.13"}`)vardatSuperNumiferr:=json.Unmarshal(byt,&dat);err!=nil{panic(err)}fmt.Printf("%+v",dat)//Iexpectthistoh

json - Go Unmarshal json,其键可以是两种类型之一

一个不在我控制范围内的api端点返回json,其中包含可以具有2种类型之一的关键人员。JSON:{"people":{"admin":{"Name":"JohnSmith","Address":"123mainstreet"},"user":{"Name":"JaneDoe","Address":"321broadstreet",}}Json类型2:{"people":[{"name":"JonSnow","pet":"Ghost"},{"name":"SansaStark","pet":"Lady",}]}对于第一种类型,我可以这样创建一个结构typePeoplestruct{Adm

json - Golang Unmarshal slice 类型接口(interface)

在此示例中,我将尝试加载包含多边形的2D场景。在代码中,我会有许多不同的结构,例如Circle、Square、Rectangle、Pentagon等。所有这些都将共享共同的功能,例如面积和周长。场景本身将存储为界面多边形的一部分。这是我用来测试的代码:packagemainimport("encoding/json""fmt""math")typePolygoninterface{Area()float32}typeRectanglestruct{Basefloat32`json:"base"`Heightfloat32`json:"height"`Xfloat32`json:"x"`

xml - 通过 UnMarshal 和 MarshalIndent 往返 xml

我想使用golang的xml.MarshalIndent()快速创建一个实用程序来格式化任何XML数据但是thiscodepackagemainimport("encoding/xml""fmt")funcmain(){typenodestruct{XMLNamexml.NameAttrs[]xml.Attr`xml:",attr"`Textstring`xml:",chardata"`Children[]node`xml:",any"`}x:=node{}_=xml.Unmarshal([]byte(doc),&x)buf,_:=xml.MarshalIndent(x,"","")/

linux - 在 192.168.65.1 :53: cannot unmarshal DNS message 上查找 google.com

我一直在让我的docker镜像发出srv记录查询。似乎golang的家伙通过忽略格式错误的记录来打破现有的行为。我听说有一个修复程序,但我一直在尝试更新版本的ubuntu/alpinelinux,但似乎没有什么不同。我无法降级到golang1.10。我在这里做错了什么吗?比如搞砸我的docker文件?我如何才能让这段代码在我的容器中实际工作?我的代码:packagemainimport("fmt""net")funcmain(){net.DefaultResolver.PreferGo=truecname,srvs,err:=net.LookupSRV("xmpp-server","t