Go   发布时间:2022-04-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了golang解析json格式大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

golang解析json格式

项目中客户端和服务端的交互数据部分为json,因此在服务端就得解析,复杂的json解析起来其实还是挺费劲的。
交互的数据类似如下格式:

{"sn":1,"ls":false,"bg":0,"ed":0,"ws":[{"bg":0,"cw":[{"sc":0,"w":"还"}]},{"bg":0,21)">"有点"}]},21)">"眼熟"}]}]}

需要将json格式中的w字段取出来,并且拼成结果串进行展示

  1. 从json数组中获取ws
  2. ws是数组,数组元素为object
  3. cw是数组,数组元素为object
  4. w是String
  5. 从cw遍历获取w字段

初步实现如下:

func RecResultJsonToPlain() {
    var recResult String
    var dat @H_289_72@map[String]interface{}
    json.Unmarshal([]byte(json_str),&dat)

    if v,ok := dat["ws"]; ok {
        ws := v.([]interface{})
        for i,wsItem := range ws {
            wsmap := wsItem.(interface{})
            if vCw,ok := wsmap["cw"]; ok {
                cw := vCw.([]interface{})
                :rgb(0,cwItem := range cw {
                    cwItemMap := cwItem.(interface{})
                    if w,ok := cwItemMap["w"]; ok {
                        recResult = recResult + w.(String)
                    }
                }
            }
        }
    }
    fmt.Println(recResult)
}

这样实现,一层一层去转换类型,再去获取元素有点麻烦。既然是已知的json数据结构,那么可以定义好结构体,再去进行解析。

type CWItem struct {
    SC int32  `json:"sc"`
    W  String `json:"w"`
}
type WSItem struct {
    CW []CWItem
}

type IatResult struct {
    SN int32    `json:"sn"`
    LS bool     `json:"ls"`
    BG int32    `json:"bg"`
    ED int32    `json:"ed"`
    WS []WSItem `json:"ws"`
}

注意定义的时候变量名第一个字母要大写,也可以使用工具来自动生成定义https://mholt.github.io/json-to-go/;用工具生成的挺漂亮:

type AutoGenerated struct {
    Sn int `json:"sn"`
    Ls bool `json:"ls"`
    Bg "bg"`
    Ed "ed"`
    Ws []struct {
        Bg "bg"`
        Cw []struct {
            Sc "sc"`
            W String `json:"w"`
        } `json:"cw"`
    } `json:"ws"`
}
func RecResultJsonToPlain(jsonResult []byte)(recPlainResult String)  {
    var r IatResult
    json.Unmarshal(jsonResult,&r)
    for _,255)">range r.WS {
        range wsItem.CW {
            recPlainResult = recPlainResult + cwItem.W
        }
    }
    return recPlainResult
}

上面的元素有json:"sn"强制说明,因此如果只需获取对应的元素,其他元素不定义也是可以的。另外还有一数据就是数组当中的元素还是数组,并且最后数组包含的是number或者String类型,需要再重写一个函数才行,数据如下,获取[21,1]当中的元素

{"Asks": [[21,1],[22,1]] ,"Bids": [[20,[19,1]]}

搜索到一段代码如下,重新实现了UnmarshalJSON

package main

import (
    "encoding/json"
    "fmt"
)

type message struct {
    Asks []Order `json:"Bids"`
    Bids []Order `json:"Asks"`
}

type Order struct {
    Price  float64
    Volume float64
}

func (o *Order) UnmarshalJSON(data []byte) error {
    var v [2]float64
    if err := json.Unmarshal(data,&v); err != nil {
        return err
    }
    o.Price = v[0]
    o.Volume = v[1]
    return nil
}

func main() {
    b := []byte(`{"Asks": [[21,1],[22,1]],"Bids": [[20,[19,1]]}`)
    var m message
    if err := json.Unmarshal(b,&m); err != nil {
        fmt.Println(err)
        return
    }
    fmt.Printf("%#v\n",m)
}


1.https://gobyexample.com/json
2.https://mholt.github.io/json-to-go/

大佬总结

以上是大佬教程为你收集整理的golang解析json格式全部内容,希望文章能够帮你解决golang解析json格式所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。