草庐IT

音频编解码

全部标签

xml - 如何将 xml 解码为接口(interface)数组?

我的应用程序中有很多结构。我想将它们全部反序列化为[]interface{}。我该怎么做?我只能为每个结构编写具体类型数组。也许任何自定义包都可以这样?这个:为此:typeRootstruct{Content[]interface{}}https://play.golang.org/p/-6hNKWdsIYn 最佳答案 HowcanIunmarshalxmlto[...]a[]interface?你不能。死的简单。包encoding/xml不支持这个。 关于xml-如何将xml解码为接口

go - 解码为接口(interface)类型

我有一些代码被丢弃了,实际上我被难住了——我以前使用过RPC和JSON方面的东西,但是当它在本地工作正常时,我似乎无法让它在RPC上工作。packagemainimport("log""net""net/rpc""net/rpc/jsonrpc""reflect")typeFoointerface{SayHello()error}typefakeFoostruct{internalValuestring}funcNewFakeFoo()*fakeFoo{f:=&fakeFoo{}f.internalValue="123456789012347"returnf}func(m*fakeFo

gob 编码到/从 *os.File 解码不工作

当我使用文件指针时f*os.File我得到一个空映射funcdecode(f*os.File,bmap[string]interface{})error{err:=gob.NewDecoder(f).Decode(&b)fmt.Printf("%+v\n",b)returnerr}funcencode(f*os.File,bmap[string]interface{})error{bb:=map[string]interface{}{"X":1,"Greeting":"hello",}err:=gob.NewEncoder(f).Encode(bb)f.Sync()//fmt.Prin

go - 将 JSON 嵌套字段值解码为结构字段

//Giventhefollowingstruct:typeMyStructstruct{FirststringSecondstringThirdstring}//IwouldliketounmarshalthefollowingJSONintoMyStructsuchas:bytes:=[]byte({{"first":{"href":"http://some/resources/1"},"second":{"href":"http://http://some/resources/2"},"third":{"href":"http://some/resources/3"}})vars

json - 在 Go 中,为什么 JSON null 有时不会传递给 UnmarshalJSON 进行解码?

Go提供了encoding/json.Unmarshaler接口(interface),因此类型可以控制它们从JSON解码的方式。在几乎所有情况下,编码后的JSON值都直接传递给UnmarshalJSON方法,但如果Unmarshaler是一个指针并且JSON值为null。在这种情况下,指针设置为nil而根本不调用UnmarshalJSON。这是一个例子:packagemainimport("encoding/json""fmt")typeTstringfunc(v*T)UnmarshalJSON(b[]byte)error{ifb[0]=='n'{*v="null"}else{*v=

json - 如何使用 Golang func json.Unmarshal 解码这个复杂的 Json 数据?

我有这样的json对象:{"action":"GetLoad","resource_id":"lb-cdvyel0v","ret_code":0,"meter_set":[{"data_set":[{"data":[[1478672400,[1,0]],[1,0],[0,0],[8,0],[1,0]],"eip_id":"eip-jf79ljt7"},{"data":[[1478693280,[0,0]],[1,0],[0,0]],"eip_id":"eip-mw6n6wg0"}],"meter_id":"uaffic"}]}我尝试这样解决问题:typeCommonResponsest

json - 在结构映射中存储/解码 json 的最佳方式

我在结构中有一个映射如下:typeRedstruct{**otherTelmap[string]string`json:"Tel"`}我收到的数据json格式如下{"Params":[{"rewew":"tref"},{"Value":"x"},....]}我正在寻找用数据填充我的结构的最有效方法,以便Tel["rewew"]="tref"Tel["Value"]="x"对于其余值,当执行此操作时这些值更简单时,它可以正常工作:vartReddecode:=json.NewDecoder(req.Body)decode.Decode(&t)但是我在使用map时遇到了问题

json - 将纪元时间戳解码为 time.Time

使用此结构运行Decode()会产生“时间戳”列错误:typeMetricsstruct{Idint`orm:"column(id);auto"`Namestring`orm:"column(name);size(255);null"json:"metric_name"`json:"lon"`Timestamptime.Time`orm:"column(timestamp);type(datetime)"json:"timestamp;omitempty"`}错误:parsingtime"1352289160"as""2006-01-02T15:04:05Z07:00"":cannot

go - 在 Go 中解码 URL

我调用的API返回UTF-8编码的XML文档中的URL。解析返回类似http://www.test.com的内容,我想将其转换为http://www.test.com.我正在努力寻找正确的方法来做到这一点。任何帮助将不胜感激!编辑:这段代码可以满足我的需要,但本以为会有一个预构建的函数来做类似于这个网站的事情:https://www.url-encode-decode.com/for_,user:=rangex.Users{a:=strings.Replace(user.Username,":",":",-1)b:=strings.Replace(a,"/","/"

json - 为非内置类型定义自定义解码

我们大多数人都知道可以使用JSON标签解码JSON对象:varjsonData=`{"name":"BrownBear"}`typeElephantstruct{Namestring`json:"name"`}这是因为string是内置类型。但是,如果Name不是内置类型,而我们想在不同的结构中使用这种类型怎么办?varjsonData=`{"name":"BrownBear"}`typeElephantstruct{NameName`json:"name"`//Unmarshallingfailshere}typeFelinestruct{NameName`json:"name"`/