Home  >  Article  >  Web Front-end  >  How to encode and decode JSON in Go language

How to encode and decode JSON in Go language

高洛峰
高洛峰Original
2017-02-04 09:52:151416browse

The example in this article describes the method of encoding and decoding JSON in Go language. Share it with everyone for your reference. The details are as follows:

json has become the best way to transmit data between different platforms. Golang has very good support for json. The code is as follows:

package main
import (
    "fmt"
    "encoding/json"
)
func main() {
    // json encode
    j1 := make(map[string]interface{})
    j1["name"] = "脚本之家"
    j1["url"] = "http://www.jb51.net/"
    js1, err := json.Marshal(j1)
    if err != nil {
        panic(err)
    }
    println(string(js1))
    // json decode
    j2 := make(map[string]interface{})
    err = json.Unmarshal(js1, &j2)
    if err != nil {
        panic(err)
    }
    fmt.Printf("%#v\n", j2)
}

I hope that this article will be useful for everyone’s Go language programs. Design helps.

For more related articles on how to encode and decode JSON in Go language, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn