赶知识网

golang解析json格式

golang 2017-08-18发布 2298次点击
项目中客户端和服务端的交互数据部分为json,因此在服务端就得解析,复杂的json解析起来其实还是挺费劲的。
 交互的数据类似如下格式:
{"sn":1,"ls":false,"bg":0,"ed":0,"ws":[{"bg":0,"cw":[{"sc":0,"w":"还"}]},{"bg":0,"cw":[{"sc":0,"w":"有点"}]},{"bg":0,"cw":[{"sc":0,"w":"眼熟"}]}]}

需要将json格式中的w字段取出来,并且拼成结果串进行展示
从json数组中获取ws
ws是数组,数组元素为object
cw是数组,数组元素为object
w是string
从cw遍历获取w字段

初步实现如下:
func RecResultJsonToPlain() {
    var recResult string
    var dat 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.(map[string]interface{})
            if vCw, ok := wsMap["cw"]; ok {
                cw := vCw.([]interface{})
                for i, cwItem := range cw {
                    cwItemMap := cwItem.(map[string]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 int `json:"bg"`
    Ed int `json:"ed"`
    Ws []struct {
        Bg int `json:"bg"`
        Cw []struct {
            Sc int `json:"sc"`
            W string `json:"w"`
        } `json:"cw"`
    } `json:"ws"`
}
func RecResultJsonToPlain(jsonResult []byte)(recPlainResult string)  {
    var r IatResult
    json.Unmarshal(jsonResult, &r)
    for _, wsItem := range r.WS {
        for _, cwItem := range wsItem.CW {
            recPlainResult = recPlainResult + cwItem.W
        }
    }
    return recPlainResult
}

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

Top10

沪ICP备09053415号 © 赶知识网