go json解析Marshal(序列化成二进制)和Unmarshal(反序列化成结构) go json解析Marshal和Unmarshal

go json解析Marshal和Unmarshal

go语言提供一个json解析的包。见 http://golang.org/pkg/encoding/json/

官方同时提供了一篇文章 JSON and Go 讲述json包的用法, 该文章同时存在中文翻译: JSON与Go 。

看过上述两篇文章后,基本使用应该就没问题了。

同时,贴几个官方的例子,方便理解。

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 . Fatal ( err ) 
        } 
        fmt. Printf ( "%s: %s \n " , m. Name , m. Text ) 
    } 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

Marshal序列化json格式

package main 
import ( 
    "encoding/json" 
    "fmt" 
    "os" 
) 
func main ( ) { 
    type ColorGroup struct { 
        ID     int 
        Name   string 
        Colors [ ] 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 ) 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

Unmarshal 反序列化json格式

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 ) 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

原文:http://studygolang.com/articles/6742

文章标签:  go

go语言提供一个json解析的包。见 http://golang.org/pkg/encoding/json/

官方同时提供了一篇文章 JSON and Go 讲述json包的用法, 该文章同时存在中文翻译: JSON与Go 。

看过上述两篇文章后,基本使用应该就没问题了。

同时,贴几个官方的例子,方便理解。

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 . Fatal ( err ) 
        } 
        fmt. Printf ( "%s: %s \n " , m. Name , m. Text ) 
    } 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

Marshal序列化json格式

package main 
import ( 
    "encoding/json" 
    "fmt" 
    "os" 
) 
func main ( ) { 
    type ColorGroup struct { 
        ID     int 
        Name   string 
        Colors [ ] 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 ) 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

Unmarshal 反序列化json格式

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 ) 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

原文:http://studygolang.com/articles/6742

猜你喜欢

转载自blog.csdn.net/weixin_40592935/article/details/80913876