Home >Backend Development >Golang >How to nest golang json
In Golang, when using JSON objects, sometimes multiple JSON objects need to be nested. This article will briefly introduce how to implement nesting of JSON objects in Golang.
JSON (JavaScript Object Notation) is a lightweight data exchange format commonly used for front-end and back-end data transmission. In Golang, JSON can be encoded and decoded using the "encoding/json" package built into the standard library.
The following is a simple JSON data example, representing a book information:
{ "title": "The Hitchhiker's Guide to the Galaxy", "author": "Douglas Adams", "price": 5.99, "publisher": { "name": "Pan Books", "address": "London" } }
In the above example, the "publisher" object is nested in the "book" object. To implement nesting of JSON objects in Golang, you need to define a structure and then use the methods provided in the standard library to encode or decode JSON data.
For example, you can define a structure named "Book" to represent book information:
type Book struct { Title string `json:"title"` Author string `json:"author"` Price float64 `json:"price"` Publisher struct { Name string `json:"name"` Address string `json:"address"` } `json:"publisher"` }
In the above code, the nested structure "Publisher" is used to represent the publication of the book information. You need to add a "json" tag to each field in the structure, so that when encoding and decoding JSON data, the field name can be correctly matched with the JSON key name.
Use the Marshal and Unmarshal methods provided by the "encoding/json" package to convert Golang structures into JSON data and JSON data into Golang structures respectively. For example, you can use the following code to convert the structure "Book" into JSON data:
book := Book{ Title: "The Hitchhiker's Guide to the Galaxy", Author: "Douglas Adams", Price: 5.99, Publisher: struct { Name string `json:"name"` Address string `json:"address"` }{ Name: "Pan Books", Address: "London", }, } jsonBytes, err := json.Marshal(book) if err != nil { fmt.Println(err) } fmt.Println(string(jsonBytes))
The output result of the above code is:
{ "title":"The Hitchhiker's Guide to the Galaxy", "author":"Douglas Adams", "price":5.99, "publisher":{ "name":"Pan Books", "address":"London" } }
In turn, you can use the following code to convert the JSON data into Structure "Book":
jsonStr := `{ "title": "The Hitchhiker's Guide to the Galaxy", "author": "Douglas Adams", "price": 5.99, "publisher": { "name": "Pan Books", "address": "London" } }` var book Book if err := json.Unmarshal([]byte(jsonStr), &book); err != nil { fmt.Println(err) } fmt.Printf("%+v ", book)
The output result of the above code is:
{Title:The Hitchhiker's Guide to the Galaxy Author:Douglas Adams Price:5.99 Publisher:{Name:Pan Books Address:London}}
Summary:
In Golang, you can use structures to implement nesting of JSON objects deal with. You need to use the "json" tag in the structure to specify the correspondence between field names and JSON key names. Use the Marshal and Unmarshal methods provided in the standard library to convert Golang structures into JSON data and JSON data into Golang structures respectively.
The above is the detailed content of How to nest golang json. For more information, please follow other related articles on the PHP Chinese website!