草庐IT

json-iterator

全部标签

json - 如何使用标签在 Go 中解码/编码 JSON?

JSON对象:{"foo_bar":"content"}代码:typePrettyStructstruct{Foostring`json:"foo_bar"`}funcwhatever(r*http.Request){varreqPrettyStructiferr:=json.NewDecoder(r.Body).Decode(&req);err!=nil{//...}log.Println(req)}这简单地输出:{}Go在解码JSON对象时不考虑我的标签,因此没有任何内容被解码到结构中,每个字段都保持零值。如果在JSON对象中,该字段被称为“foo”或“Foo”,则一切正常。我已经

arrays - 在 Go 中解码顶级 JSON 数组

我正在通过编写一个简单的http服务器来学习Go,我需要处理一些JSON响应。有了对象响应,我可以用两行代码按照惯用的方式解码它:结构结果:=富{}json.Unmarshal(structBody,&structResult)我不知道如何对数组响应做同样的事情(见下面的例子)。有没有一种方法可以指定(可能通过json标记)顶级数组应该进入给定的结构字段?packagemainimport"fmt"import"encoding/json"typeFoostruct{Iduint64`json:"id"`Namestring`json:"name"`}typeBaseResultstr

json - 在 json.Marshal() 中指定结构格式

我有以下结构,用于与API通信:typeObjectstruct{Iduint64TypestringClassstringProperties[]Property}typePropertystruct{NamestringDataTypestringValueinterface{}}我使用json.MarshalIndent()在发送之前将我的结构转换为json。这给了我类似的东西:{"Id":15,"Type":"Node","Class":"Persona","Properties":[{"Name":"Nombre","DataType":"text","Value":"Oso

Go:将空结构编码为 json

我正在尝试将结构编码为json。它在结构具有值时起作用。但是,当结构没有值时,我无法访问网页:开始:typeFruitsstruct{Apple[]*Description'json:"apple,omitempty"'}typeDescriptionstruct{ColorstringWeightint}funcHandler(whttp.ResponseWriter,r*http.Request){j:={[]}js,_:=json.Marshal(j)w.Write(js)}错误是因为json.Marshal无法编码空结构吗? 最佳答案

json - Go:指向接口(interface){} 的指针丢失底层类型

我正在使用Go中的一些“通用”函数,这些函数在interface{}上运行并在channel周围发送东西,等等。精简一下,假设我有类似的东西:typeMyTypestruct{//Fields}func(m*MyType)MarshalJSON()([]byte,error){//MarshalJSONlog.Print("customJSONmarshal")return[]byte("hello"),nil}funcGenericFunc(vinterface{}){//Dothings...log.Print(reflect.TypeOf(v))log.Print(reflect

arrays - 如何从 go map 对象访问深度嵌入的 json 对象?

我是新手,我了解如何将json数据编码为自定义预定义结构类型,但我目前正在使用一个JSON集,它可以在每次调用时具有动态键和值。我可以将这些动态数据编码到map/界面中,没问题,但我对如何访问深度嵌套在数组中的项目有点迷茫。这是我在USDOL网站上使用的示例JSON{"name":"osha-establishment","count":15,"frequency":"ManualCrawl","version":4,"newdata":true,"lastrunstatus":"success","lastsuccess":"MonDec08201411:19:57GMT+0000(

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"

json - 指向 JSON 的指针数组

在golang中,我有指向结构的二维指针slice,如下面的代码所示。typepointstruct{xintyint}typecellstruct{pointpointvisitedboolwallswalls}typewallsstruct{nbooleboolsboolwbool}typemazestruct{cells[][]*cellsolutionStackStack}我想将单元格slice序列化为JSON。但由于所有元素都是指针,因此调用编码将给出空JSON。序列化此slice的最佳方法是什么。我想到的一个解决方案是创建此2Dslice的本地副本,并将所有指针替换为实际结

json - 如何在 go lang 中对 map[string]interface{} 类型进行多重排序?

场景:假设我有一个JSON数据要在golang中处理现在我正在使用map[string]interface{}类型,通过执行marshal/unmarshal使用packageencoding/json下面是JSON数据:{"MysoreCity":{"Population":1000,"VehicleCount":1700,"Temperature":33},"BangaloreCity":{"Population":1000,"VehicleCount":3500,"Temperature":33},"KolarCity":{"Population":1250,"VehicleCo

xml - Go 中如何将 XML 数据转换为 JSON 数据?

我想在Go中从XML文档创建JSON对象。现在我正在做的是使用xml.Unmarshall函数获取结构对象中的XML数据,然后使用fmt.Sprintf函数以编程方式格式化JSON结构中的字符串。这段代码不可读,我觉得应该有更好的方法来做到这一点。有人可以提出更好的建议吗。我当前的代码是varrootRoot_=xml.Unmarshal(data,&root)fmt.Fprintln(w,fmt.Sprintf("{\"type\":\"%s\",\"action\":\"save\",\"entry\":{\"ads_enabled\":1,\"comments_enabled\"