go json解析Marshal和Unmarshal

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

http://www.tuicool.com/articles/BFruI3

 

 

go語言提供一個json解析的包。見 http://golang.org/pkg/encoding/json/

官方同時提供了一篇文章 JSON and Go 講述json包的用法, 該文章同時存在中文翻譯: JSON與Go 。

看過上述兩篇文章後,基本使用應該就沒問題了。

同時,貼幾個官方的例子,方便理解。
Decoder:

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
31
32

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 )
    }
}

Marshal:

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

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 )
}

RawMessage :

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

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 . 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 . Fatalln ( "error:" , err )
        }
        fmt. Println ( c. Space , dst )
    }
}

Unmarshal:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

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 )
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.