Home > Article > Backend Development > How to read json data in golang
JSON (JavaScript Object Notation) is a more lightweight data exchange format than XML. It is easy for people to read and write, and it is also easy for programs to parse and generate.
Go languageBuilt-in support for JSON. Using the encoding/json standard library built into the GO language, developers can easily use GO programs to generate and parse data in JSON format.
Example:
package main import ( "encoding/json" "fmt" ) type Book struct { Title string Author []string Publisher string Price float64 IsPublished bool } func main() { b := []byte(`{ "Title":"go programming language", "Author":["john","ada","alice"], "Publisher":"qinghua", "IsPublished":true, "Price":99 }`) //先创建一个目标类型的实例对象,用于存放解码后的值 var book Book err := json.Unmarshal(b, &book) if err != nil { fmt.Println("error in translating,", err.Error()) return } fmt.Println(book.Author) }
The Json.Unmarshal() function will search for fields in the target structure according to an agreed order, and match if one is found. These fields must all be exportable fields starting with a capital letter in the type declaration.
For more golang knowledge, please pay attention to the golang tutorial column on the PHP Chinese website.
The above is the detailed content of How to read json data in golang. For more information, please follow other related articles on the PHP Chinese website!