English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Go has built-in support for JSON encoding and decoding. It also supports custom data types.
The Marshal function is used to convert go data types to JSON format.
The syntax of the Marshal function is:
"func Marshal(v interface{}) ([]byte, error)"
Marshal returns the JSON encoding of v.
Boolean values are converted to JSON boolean values. Floating-point numbers, integers, and numbers are converted to JSON numbers. The key type of a map must be a string, an integer type, or implement encoding.TextMarshaler.
JSON decoding is completed using the Unmarshal function.
The syntax of the Unmarshal function is:
"func Unmarshal(data []byte, v interface{}) error"
Unmarshal decodes the JSON-encoded value and stores the result in the value pointed to by v. If v is nil or not a pointer, Unmarshal returns InvalidUnmarshalError.
We can also customize the fields stored in the struct field tags under the "json" key. We can use the name of the field, followed by a comma-separated list of options.
Field int 'json:"myName"' // Displayed as key "myName" in JSON. Field int 'json:"myName,omitempty?'//If the value of this field is empty, the field is omitted from the object. Field int 'json:""-"" ////Fields are ignored by this package.
package main import "encoding"/"json" import "fmt" func main() { bolType, _ := json.Marshal(false) //Boolean value fmt.Println(string(bolType)) intType, _ := json.Marshal(10) // Integer value fmt.Println(string(intType)) fltType, _ := json.Marshal(3.14) //Float value fmt.Println(string(fltType)) strType, _ := json.Marshal("w"3codebox") // String value fmt.Println(string(strType)) slcA := []string{"sun", "moon", "star"} //Slice value slcB, _ := json.Marshal(slcA) fmt.Println(string(slcB)) mapA := map[string]int{"sun": 1,"moon": 2} //Map value mapB, _ := json.Marshal(mapA) fmt.Println(string(mapB)) }
Output:
false 10 3.14 "w"3codebox" ["sun","moon","star"] {"moon":2,"sun":1}
package main import ( "encoding"/"json" "fmt" "os" ) type Response1 struct { Position int Planet []string } type Response2 struct { Position int Planet []string 'json:"planet"' } func main() { res1A := &Response1{ Position: 1, Planet: []string{"mercury", "venus", "earth"}} res1B, _ := json.Marshal(res1A) fmt.Println(string(res1B)) res2D := &Response2{ Position: 1, Planet: []string{"mercury", "venus", "earth"}} res2B, _ := json.Marshal(res2D) fmt.Println(string(res2B)) byt := []byte('{"pi":6.13,"place":["New York","New Delhi"]}`) var dat map[string]interface{} if err := json.Unmarshal(byt, &dat); err != nil { panic(err) } fmt.Println(dat) num := dat["pi"].(float64) fmt.Println(num) strs := dat["place"].([]interface{}) str1 := strs[0].(string) fmt.Println(str1) str := `{"Position": 1, "Planet": ["mercury", "venus"]}` res := Response2{} json.Unmarshal([]byte(str), &res) fmt.Println(res) fmt.Println(res.Planet[1]) enc := json.NewEncoder(os.Stdout) d := map[string]string{"1:"mercury" , "2: "venus"} enc.Encode(d) }
Output:
{"Position":1,"Planet":["mercury","venus","earth"]} {"position":1,"planet":["mercury","venus","earth"]} map[pi:6.13 place:[New York New Delhi]] 6.13 New York {1 [mercury venus]} venus {"1"mercury","2:"venus"