jjzjj

Marshalling

全部标签

xml - 读取类型的属性 XMLName 属性值

我正在使用我自己的MarshalXML函数来获取输入对象所需的输出格式,但似乎无法找到为标签赋予适当名称的适当方法。假设我有:typeRootElementstruct{XMLNamexml.Name`xml:"hello"`worldChildElement`xml:"world"`}还有一个子元素:typeChildElementstruct{Valuestring}然后在MarshalXML中为RootElement编码方法会给我:...我必须创建自己的EncodeToken方法调用集,而不是那个id,以便将正确的标签名称放在那里,但即使在这里,我也必须将xml.Name指定为常

go - 如何使用 Golang 中的数字枚举以用户友好的 JSON 格式保存节俭结构?

Task.thrift(Thrift版本0.9.3)enumAttributeApp{a=1,b=2,c=3}typedefi32attrTypeIdstructTask{1:requiredattrTypeIdtype_id,2:requiredlistapp_to,}为Java编码枚举生成ApacheThrift代码。$thrift-r--genjavaTask.thriftTSerializerserializer=newTSerializer(newTSimpleJSONProtocol.Factory());Stringjson=serializer.toString(tas

mysql - 乱码 : json of json not work

示例:{"id":1"data":{"1":2}}结构定义:typeItemstruct{idint`json:"id"`datainterface{}`json:"data"`}我需要解析来自httppost的负载,所以我使用interface{}作为data,json.Unmarshal()是成功,但gorm在调用db.Create(item)时产生错误:(sql:convertingExecargument#5'stype:unsupportedtypemap[string]interface{},amap)相反,我将interface{}更改为string,调用json.Unm

json - easyjson 将数组解码为 go 结构

我最近开始使用golang和easyjson。现在我必须将一个json数组解码为一个结构才能使用它。这就是我得到的。传入的JSON数据如下所示[{"Item1":true,"Item2":"hello","Item3":{"A":1,},},...]我的结构:packagesomething//easyjson:jsontypeItemstruct{Item1boolItem2stringItem3SubItem}//easyjson:jsontypeSubItemstruct{Aint}(我构建了*_easyjson.go文件)下面是我将如何使用easyjson:funcConver

xml - 在 Go 中编码(marshal) XML 时如何保留标记的 namespace url

当我解码和编码此XML时,命名空间的URL消失了:tekst变成:代码:packagemainimport("encoding/xml""fmt")typeRootstruct{XMLNamexml.Name`xml:"root"`XmlNSstring`xml:"xmlns:urn,attr"`CopyrightCopyright`xml:"urn:copyright,omitempty"`}typeCopyrightstruct{Textstring`xml:",chardata"`}funcmain(){root:=Root{}x:=`text`_=xml.Unmarshal([

arrays - Golang 通用 JSON 编码

我正在从事基于JSON通信的小型服务器-客户端项目。但我遇到了问题。我正在尝试创建一个具有通用消息正文的响应结构。这意味着我有一个map,其中一个键作为字符串,一个json原始消息作为值。最后,消息正文应该适用于任何类型(字符串、整数、数组)packagemainimport("encoding/json""fmt")typeServerResponsestruct{Codeint`json:"code"bson:"code"`Typestring`json:"type"bson:"type"`Bodymap[string]json.RawMessage`json:"body"bson

go - 为空自定义类型省略 JSON

我正在尝试为可能的nil数据库类型编写自定义编码(marshal)拆收器。它的结构与sql.NullFloat64类型完全相同:typeNullFloat64sql.NullFloat64func(ni*NullFloat64)MarshalJSON()([]byte,error){if!ni.Valid{return[]byte("null"),nil}returnjson.Marshal(ni.Float64)}它是要编码的较大结构的一部分:typeDatastruct{Xtime.Time`json:"x"`Yfloat32`json:"y"`StderrNullFloat64`

json - golang - json HTML 转义

尝试使用自定义MarshalJSON来避免像&这样的转义字符。在这种特殊情况下,使用带有SetEscapeHTML选项的自定义编码器是不方便的。该函数被调用,并且按我预期的那样运行。然而,最终结果仍然包含转义形式\u0026。https://play.golang.org/p/aktPndOUmthpackagemainimport("bytes""encoding/json""fmt")typeDatastruct{URLstring}func(dData)MarshalJSON()([]byte,error){data:=map[string]interface{}{"url":d

Golang 形式编码嵌套结构

我正在使用ajg/form包将我的嵌套结构编码为url编码数据。packagemainimport("fmt""bytes""github.com/ajg/form")typeSubjectstruct{SubjectTagstring`form:"tag,omitempty"`SubjectNamestring`form:"name,omitempty"`}typeStudentstruct{Namestring`form:"stud_name,omitempty"`SubjectList[]Subject`form:"subjects,omitempty"`}funcmain(){

未导出类型的 JSON 编码

我想使用未导出的自定义类型并定义一个接口(interface)来使用它。自定义类型将实现该接口(interface),除了从JSON解码外,一切正常。我创建了一个例子来更好地解释它:type(Valueinterface{Set(k,vstring)}valuemap[string]string)funcNew()Value{returnvalue{}}func(valvalue)Set(k,vstring){val[k]=v}这种方法不会提供对map的直接访问,也不会强制使用New()函数来创建“Value”类型的对象。当尝试在使用New()创建的对象中解码时,出现错误:Failed