go 對 json 的處理


json不用多說,一種跨語言的交換協議,這里探討一下Go語言對json的支持。

Go對於json有官方自己的解析包,先談一下json的解碼方式。解碼的api如下:

func Unmarshal (data []byte, v interface{})

在go中,json解碼的數據結構有兩種,一種是解析到結構體,一種是解析到空的interface。

以數據 {"changes": [{"index":5, "armid":6},{"index":9,"armid":10}]} 為例

1,解析到結構體

package main
   
import (
     "encoding/json"
    "fmt"
)

func main() {
    type change struct {
        Index  int
        Armid  int
    }

    type change_slice struct {
        Changes  []change
    }

    var msg change_slice

    str := `{"changes": [{"armid":3,"Index":5}, {"armid":3,"Index":6}]}`
    err := json.Unmarshal([]byte(str), &msg)
    if err!=nil {
        fmt.Println("Can't decode json message",err)
    }
    fmt.Println(msg)

    for _, change := range msg.Changes {
        fmt.Println(change.Armid, change.Index)
    }
}

 

運行結果是:

{[{5 3} {6 3}]}
3 5
3 6

 

需要注意的是:

(1)解析到結構體時只能解析到結構體中的首字母大寫的變量

(2)結構體中數值對應json中的key是大小寫不敏感的

2,解析到interface

package main 

import (
    "encoding/json"
    "fmt"
)

func main() {
    str := `{"changes": [{"armid":3,"Index":5}, {"armid":3,"Index":6}]}`
    var msg map[string]interface{}
    err := json.Unmarshal([]byte(str), &msg)
    if err!=nil {
        fmt.Println("fuck",err)
    }
    fmt.Println(msg)
    
    changes,ok := msg["changes"].([](interface{}))
    if !ok {
        fmt.Println("Can't convert msg to []interface")
    }
    
    for _ ,ichange := range changes {
        change, ok := ichange.(map[string]interface{})
          if !ok {
              fmt.Println("cant convert ichange to map[string]interface{}")
          }
        Index, ok := change["Index"].(float64)
        if !ok {    
            fmt.Println("cant convert Index to float64")
        }
        armid,ok := change["armid"].(float64)
        if !ok {    
            fmt.Println("cant convert armid to float")
        }
        fmt.Println(Index,armid)
    }
}

運行結果是:

map[changes:[map[armid:3 Index:5] map[armid:3 Index:6]]]
5 3
6 3

需要注意的是:

(1)json中的數字解碼到map中統一是float64類型,如果實際中采用其它類型需要手動轉換

(2)map中對應json的key是大小寫敏感的,同時也沒有結構體中要求的首字母大寫

 

總結:

比較一下可以發現,在已知數據結構的情況下,采用結構體方式解碼在對解碼后的取值上要方便許多,而采用空接口要經過多次斷言,適合使用在不知道數據格式的情況下。


免責聲明!

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



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