jsonencoding/json
1 序列化为json字符串

1.1 Marshal

package main 
import ( 
    "encoding/json" 
    "fmt" 
    "os" 
) 
func main ( ) { 
    type ColorGroup struct { 
        ID     int 
        Name   string `json:"name"`
        Colors [ ] string 
    note   string
    } 
    group := ColorGroup { 
        ID :     1 , 
        Name :   "Reds" , 
        Colors : [ ] string { "Crimson" , "Red" , "Ruby" , "Maroon" } , 
    } 
    b , err := json. Marshal ( group ) 
    if err != nil { 
        fmt. Println ( "error:" , err ) 
    } 
    os. Stdout . Write ( b ) 
}

结果输出:

{"ID":1,"name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}

1.2 序列化备注

notejsonjson:"name"nameNameIDColors
boolchannelcomplex函数
interface{}stringintbool指针类型interface{}
2 反序列化

2.1 Unmarshal

package main 
import ( 
    "encoding/json" 
    "fmt" 
) 
func main ( ) { 
    var jsonBlob = [ ] byte ( ` [ 
        { "Name" : "Platypus" , "Order" : "Monotremata" } , 
        { "Name" : "Quoll" ,     "Order" : "Dasyuromorphia" } 
    ] ` ) 
    type Animal struct { 
        Name  string 
        Order string 
    } 
    var animals [ ] Animal 
    err := json. Unmarshal ( jsonBlob , & animals ) 
    if err != nil { 
        fmt. Println ( "error:" , err ) 
    } 
    fmt. Printf ( "%+v" , animals ) 
}

结果输出:

[{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]

2.2 RawMeaage

package main 
import ( 
    "encoding/json" 
    "fmt" 
    "log" 
) 
func main ( ) { 
    type Color struct { 
        Space string 
        Point json. RawMessage // delay parsing until we know the color space 
    } 
    type RGB struct { 
        R uint8 
        G uint8 
        B uint8 
    } 
    type YCbCr struct { 
        Y   uint8 
        Cb int8 
        Cr int8 
    } 
    var j = [ ] byte ( ` [ 
        { "Space" : "YCbCr" , "Point" : { "Y" : 255 , "Cb" : 0 , "Cr" : -10 } } , 
        { "Space" : "RGB" ,   "Point" : { "R" : 98 , "G" : 218 , "B" : 255 } } 
    ] ` ) 
    var colors [ ] Color 
    err := json. Unmarshal ( j , & colors ) 
    if err != nil { 
        [log ](http://www.opengroup.org/onlinepubs/009695399/functions/log.html). Fatalln ( "error:" , err ) 
    } 
    for _ , c := range colors { 
        var dst interface { } 
        switch c. Space { 
        case "RGB" : 
            dst = new ( RGB ) 
        case "YCbCr" : 
            dst = new ( YCbCr ) 
        } 
        err := json. Unmarshal ( c. Point , dst ) 
        if err != nil { 
            [log ](http://www.opengroup.org/onlinepubs/009695399/functions/log.html). Fatalln ( "error:" , err ) 
        } 
        fmt. Println ( c. Space , dst ) 
    } 
}

输出结果:

YCbCr &{255 0 -10}
RGB &{98 218 255}

2.3 Decoder

package main 
import ( 
    "encoding/json" 
    "fmt" 
    "io" 
    "log" 
    "strings" 
) 
func main ( ) { 
    const jsonStream = ` 
        { "Name" : "Ed" , "Text" : "Knock knock." } 
        { "Name" : "Sam" , "Text" : "Who's there?" } 
        { "Name" : "Ed" , "Text" : "Go fmt." } 
        { "Name" : "Sam" , "Text" : "Go fmt who?" } 
        { "Name" : "Ed" , "Text" : "Go fmt yourself!" } 
    ` 
    type Message struct { 
        Name , Text string 
    } 
    dec := json. NewDecoder ( strings. NewReader ( jsonStream ) ) 
    for { 
        var m Message 
        if err := dec. Decode ( & m ) ; err == io. EOF { 
            break 
        } else if err != nil { 
            [log ](http://www.opengroup.org/onlinepubs/009695399/functions/log.html). Fatal ( err ) 
        } 
        fmt. Printf ( "%s: %s \n " , m. Name , m. Text ) 
    } 
}

输出结果:

 Ed: Knock knock. 
 Sam: Who's there? 
 Ed: Go fmt. 
 Sam: Go fmt who? 
 Ed: Go fmt yourself! 

2.4 反序列化备注

  1. json字符串解析时,需要一个接收体接收解析后的数据,且Unmarshal时接收体必须传递指针。否则解析虽不报错,但数据无法赋值到接收体中。
  2. json字符串key和对象字段的匹配规则
    解析时,接收体可自行定义。json串中的key自动在接收体中寻找匹配的字段进行赋值。匹配规则是:
  • 先查找与key一样的json标签,找到则赋值给该标签对应的变量(如Name)。
  • 没有json标签的,就从上往下依次查找变量名与key一样的变量,如Age。或者变量名忽略大小写后与key一样的变量,如HIgh,Class。第一个匹配的就赋值,后面就算有匹配的也忽略。
  • 可解析的变量必须是可导出的,即首字母大写。不可导出的变量无法被解析(即使json串中有对应key的k-v,解析后其值仍为nil,即空值)。
  • 当接收体中存在json串中匹配不了的项时,解析会自动忽略该项,该项仍保留原值。如没有初始值,保留空值nil。
  • interface{}类型的变量,如果解析时没有明确指定字段的类型,可能得不到自己期望的数据结构。例如:解析时不指定变量的具体类型(定义为interface{}类型),json自动将value为复合结构的数据解析为map[string]interface{}类型的项。
reflect.TypeOf(value)nilinterface{}interface{}map[string]interface{}interface{}interface{}json.RawMessagejson.RawMessagemap[string]interface{}
参考