草庐IT

unmarshal

全部标签

json - Unmarshal 中的动态类型

我目前遇到以下问题:我通过websocket得到一个[]byte/string看起来像eventname{"JSON":"data","In":"differentformats"}我用事件名称和JSON数据之间的空格拆分字符串,根据事件名称,我想将json.Unmarshal()JSON数据转换为特定类型或var一个特定的类型,以确保它的所有类型都是安全的。所以我可能会有一个映射,其中包含所有可能的事件名称和JSON数据的相应类型,但我不确定我将如何保存类型,可能是通过引用还是通过字符串化名称?typeEventTypeListmap[string]*interface{}或type

amazon-web-services - DynamoDB Marshal 和 unmarshal golang time.Time 自纪元以来以毫秒为单位

SDK默认将time.Time值编码为RFC3339字符串。您如何选择以其他方式编码和解码,例如自纪元以来毫秒?SDK提到了Marshaler和Unmarshaler接口(interface),但没有解释如何使用它们。 最佳答案 (当我正要发布我的问题时,我通过研究UnixTime的工作原理找到了答案)。要使用自定义Marshaler和Unmarshaler,您可以创建自定义类型。typeMillisTimetime.Timefunc(eMillisTime)MarshalDynamoDBAttributeValue(av*dyna

xml - 接口(interface)和编码/xml Unmarshal

我有一个soap服务,我正在写反对。soapAPI的一部分用于返回查询结果,我希望提供用于解码信封的基本结构,同时允许开发人员填写encoding/xml将解码到的接口(interface)。typeQueryEnvelopestruct{XMLNamexml.Name`xml:"http://schemas.xmlsoap.org/soap/envelope/Envelope"`Body*QueryBody`xml:"http://schemas.xmlsoap.org/soap/envelope/Body"`}typeQueryBodystruct{QueryResult*Quer

json - golang : json. Unmarshal() 返回 "invalid memory address or nil pointer dereference"

我从websocket收到一条json消息,json字符串接收正常。然后我调用json.Unmarshal并引发运行时panic。我查看了其他示例,但这似乎是另一回事。这是代码:functranslateMessages(ssocket){message:=make([]byte,4096)for{fmt.Printf("Waitingforamessage...\n")ifn,err:=s.Read(message);err==nil{command:=map[string]interface{}{}fmt.Printf("Receivedmessage:%v(%dBytes)\n"

go - 不同类型的相同方法并在 Go 中返回不同的类型值

我见过一些类似的问题(SamemethodondifferentarraytypesinGo)但在我的例子中,我的函数不返回相同的类型。你能把下面的代码写得更简单点吗?packagemainimport("encoding/json""fmt")typeAstruct{Namestring`json:"name"`Ageint`json:"age"`}typeBstruct{Namestring`json:"name"`Ageint`json:"age"`Addressstring`json:address`}funcUnmarshalA(b[]byte)*A{vart*A_=json

JSON-RPC, "cannot unmarshal object"

我正在尝试了解JSON-RPC的工作原理,并正在使用Go语言(golang)对其进行测试。Go程序运行良好。它做了它应该做的。但是当我尝试通过telnet发出原始请求时,它给出了一个错误。这里描述了工作和super简单的JSON-RPC服务器://rpc_json_server.gopackagemainimport("log""net""net/http""net/rpc""net/rpc/jsonrpc")//------------------------------------------------------------------------------//Types//

xml - Go XML Unmarshaling 不读取属性

Go语言的新手。从XML中,代码解码除属性之外的所有值。有人可以告诉我做错了什么吗:packagemainimport("encoding/xml""fmt")funcmain(){v,_:=GetData()fmt.Print(v)}typeQuerystruct{InstituationList[]Instituation`xml:"institution"`}typeInstituationstruct{XMLNamexml.Name`xml:"institution"`OFXHomeIDstring`xml:"id,attr"`Namestring`xml:"name"`FId

Go xml.Unmarshal 仅获取列表的最后一项

那里!我正在解析xml文档并将其内容解码到结构中,但它只返回列表中的最后一项而不是完整列表。列表是serverList并且在解码后它只返回最后一个server实例。需要帮助。funcmain(){xmlFile:=`01Main1.1.1.1808025truetrue2Reg11.1.1.2808025falsefalse`typeserverInfostruct{ServerIDstring`xml:"serverId"`NauServerstring`xml:"nauServer"`ServerIPstring`xml:"serverIp"`ServerPortint`xml:"

json - Marshal/Unmarshal int 类型

我使用int类型来表示枚举。当我将它编码为JSON时,我想将它转换为字符串,据我所知,我应该实现UnmarshalJSON和MarshalJSON,但它提示:marshalerror:json:errorcallingMarshalJSONfortypemain.trxStatus:invalidcharacter'b'lookingforbeginningofvalueunexpectedendofJSONinput编码时。然后我将引号添加到编码字符串中:func(strxStatus)MarshalJSON()([]byte,error){return[]byte("\""+s.S

go - 当字段实现 UnmarshalJSON 时,Unmarshal 嵌入式字段指针会出现困惑

我有一个struct,它嵌入了一个指向另一个struct的嵌入式指针。当我使用默认的json.Unmarshal行为时,它工作得很好。但是当我为embeddedstruct的类型实现UnmarshalJSON而不是外部struct,然后使用空指针解引用进行panic。如果我也为外部struct类型实现UnmarshalJSON,那么它就可以工作。但是,外部结构有许多字段,我宁愿不必手动解码。为什么在一个而不是另一个上实现UnmarshalJSON会导致panic?有没有办法在不为外部类型实现UnmarshalJSON的情况下让它工作?如果没有,是否有更简单/更少手动的方法来为外部类型实