Use of JSON in Golang

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

Using JSON in Golang, you often use two of functions

Func unmarshal (data []byte, v interface{}) error

1 deserializing JSON into a struct object

2 deserializing JSON into map

3 deserialization of JSON into slice


Func Marshal (v interface{}) ([]byte, error)
1 serializing a struct object into JSON

2 serializing a map into JSON

3 serializing the slice into JSON


Package Main

Import (

"FMT"

"Encoding/json"

)

Type User struct {

Username string

Password string

Friendname []string

}

Func Main () {

user:=user{}

User. Username= "Tom"

User. Password= "123456"

User. friendname=[]string{"Li", "Fei"}

       // will be struct turn into JSON String, Note: The first letter of a field in a struct must be capitalized, otherwise it cannot be resolved

If Userjson,err:=json. Marshal (user); err==nil{

Fmt. Println (String (Userjson))// print Result:{"Username": "Tom", "Password": "123456", "Friendname": ["Li", "Fei"]}

}

turn slice into a json string

arr:=[]string{"Apple", "Orange", "Banana"}

If Arrjson,err:=json. Marshal (arr); err==nil{

Fmt. Println (String (Arrjson))// print Result:["Apple", "Orange", "Banana"]

}

Turn map into a json string

m:=map[string]string{" Zhejiang ": " Hangzhou ", " Hunan ": " Changsha "}

If Mjson,err:=json. Marshal (m); err==nil{

Fmt. Println (String (Mjson))//print Result: {" zhejiang ": " Hangzhou ", " Hunan ": " Changsha "}

}

JSON into a struct

Jsonstr:= ' {"Username": "Tom", "Password": "123456", "Friendname": ["Li", "Fei"]} '

var Userjson User

If Err:=json. Unmarshal ([]byte (JSONSTR), &userjson); err==nil{

Fmt. PRINTLN (Userjson)// print Result:{Tom 123456 [Li Fei]}

}

JSON turns into slice

Jsonfruit:= ' ["Apple", "Orange", "Banana"] '

var arrfruit []string

If Err:=json. Unmarshal ([]byte (Jsonfruit), &arrfruit); err==nil{

Fmt. PRINTLN (arrfruit)// print Result:[Apple Orange Banana]

}

JSON turns into a map

Jsoncity:= ' {" zhejiang ": " Hangzhou ", " Hunan ": " Changsha "} '

var mapcity map[string]string

If Err:=json. Unmarshal ([]byte (jsoncity), &mapcity); err==nil{

Fmt. PRINTLN (mapcity)// print Result: map[Zhejiang: Hangzhou Hunan: Changsha]

}

}




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.