草庐IT

Unmarshalling

全部标签

go - 将 JSON 解码为结构 - 列表中的列表类型?

我正在尝试将JSON对象解码为Go中的结构。这是JSON对象:{"configuration":{"currentpowersource":"","sensorcatalogue":[[],[],[],[]],"actuatorcatalogue":[[],[],[],[]],"activeinterface":""}}这是Go中的结构:typeDatastruct{Configurationstruct{CurrentPowerSourcestring`json:"currentpowersource"`SensorCatalogue//whatisthetypeinGoforlis

未导出类型的 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

json - 无法将外部结构直接调用到 map[string]struct

我一直在努力找出在解码JSON时为map[string]struct类型调用外部结构的正确方法。当所有代码都在同一个包中时,代码可以正常工作,但是如果它正在提取导出的类型,那么Unmarshal函数似乎有错误。packageanimalstypeBirdstruct{Namestring`json:"name"`Descriptionstring`json:"description"`}packagemainimport("encoding/json""fmt""../animal")funcmain(){birdJson:=`{"birds":{"name":"eagle","des

json - 从 map 结构保存读取/写入/保存到 json

我一直在尝试拥有一个“工作”文件,我将我的应用程序的某些基本状态保存到其中,而不是将它们保存在Ram中,因为它们需要每天保存,我决定每天创建一个文件,这部分工作正常,但为了更清楚起见,我已将其从代码中删除。现在我可以使用信息结构的假值初始化我的文件,然后解码并从中读取。当我尝试更新"file"后,在我将其保存回文本文件之前已解码时,问题就出现了。isImportStarted确实有效(删除错误行obv时)但我似乎无法正确更新文件,我收到此错误:./test.go:62:34:cannotassigntostructfieldTheList[symbol].ImportStartedin

casting - 如何将未编码的 Golang 对象转换为指定变量的类型

我想将各种对象编码到文件中,然后解码它们,并通过获取编码的变量类型将它们转换回它们的原始类型。关键是我想将未编码的对象转换为指定变量的类型,而不指定类型。简短的伪代码://Marshalthisitem:=Book{"TheMythofSisyphus","AlbertCamus"}//Thenunmarshalandconverttothetypeoftheitemvariable.itemType:=reflect.TypeOf(item)newItemitemType=unmarshalledItem.(itemType)//Thisistheproblem.fmt.Printl

xml - Golang 不时解码 mysql 格式。时间

我在XML文档中有一个格式为2016-06-1622:21:00的时间。我想用Golang解析那个时间。typePricestruct{Instrumentstring`xml:"Instrument"`Bidfloat32`xml:"Bid"`Askfloat32`xml:"Ask"`Updatedtime.Time`xml:"Updated"`}typePrices[]PricevarpPriceserr:=xml.Unmarshal(body,&p)iferr!=nil{log.Panicln(err)}我的输出错误如下:panic:parsingtime"2016-06-162

arrays - 在 Go/Golang 中解码嵌套的 JSON 数组

您好,我在解码嵌套的JSON数组时遇到问题。我应该创建什么结构?我想尽可能避免使用interface{},但我真的不知道在这种情况下是否可行。我要解码的Json:"[[[{\"aaa\":\"aaa\"}]]]"和我想用来解码的结构:typeSomeStructNestedNestedstruct{Aaastring`json:"aaa"`}typeSomeStructNestedstruct{SomeStructNestedNested[]SomeStructNestedNested}typeSomeStructstruct{SomeStructNested[]SomeStructN

json - 如何正确解码 Golang 中的无键 JSON 数组?

我有如下所示的JSON数据:[{"globalTradeID":64201000,"tradeID":549285,"date":"2016-11-1123:51:58","type":"buy","rate":"10.33999779","amount":"0.02176472","total":"0.22504715"},{"globalTradeID":64200631,"tradeID":549284,"date":"2016-11-1123:48:39","type":"buy","rate":"10.33999822","amount":"0.18211700","tota

Golang RethinkDB ChangeFeed 结构

我想知道是否有人可以解释如何将我的changefeed游标值解码为特定的结构类型。varmessagemap[string]interface{}forchatFeedCursor.Next(&message){fmt.Println(message)}map[new_val:map[club_id:ea2eb6e2-755f-4dad-922d-e3693b6e55c6date:2017-04-0714:48:17.714+0100+01:00id:e389ab54-963e-4b33-9b34-adcb6ec5b17emessage:whatisthemeaningoflife?u

json - 尝试解码映射[优先级]字符串

我一直在研究Golangjson包,遇到了一个问题,我只是想将map[string]string解码为map[Priority]string最简单的方法,但似乎我的UnmarshalJSON甚至没有运行,以下是我的代码,我想制作一个可以运行的Playground,但我正在使用操作系统包:import("encoding/json""fmt""os")typePriorityintconst(VERYLOW=Priority(iota)LOWMEDIUMHIGHVERYHIGH)const(verylow="verylow"low="low"medium="medium"high="hi