Go JSON parsing marshal and Unmarshal

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

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

The Go language provides a JSON-parsed package. See http://golang.org/pkg/encoding/json/

The official also provided an article JSON and go about the use of the JSON package, the article also exists in Chinese translation: JSON and go.

After reading the above two articles, the basic use should be no problem.

At the same time, put a few official examples, easy to understand.
Decoder:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
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?"}
{"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
Ten
All

16

(
)
,
,
,
,
,
,

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
21st
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
21st
22
23

Package main
Import (
    "Encoding/json"
    "FMT"
)
Func main () {
    var jsonblob = [] Byte (' [
        {"Name": "Platypus", "Order": "Monot Remata "},
        {" Name ":" Quoll ",    " Order ":" Dasyuromorphia "}
  & nbsp ] ')
    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)
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.