golang解析json格式 -- 全


項目中客戶端和服務端的交互數據部分為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字段取出來,並且拼成結果串進行展示

  1. 從json數組中獲取ws
  2. ws是數組,數組元素為object
  3. cw是數組,數組元素為object
  4. w是string
  5. 從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) }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM