In Go, you can use the built-in encoding/json package to handle JSON format data. This package provides functions and types that enable you to parse JSON data into Go objects (deserialized) or convert Go objects into JSON data (serialized).
Here are some common JSON processing operations:
Deserialization (parse JSON)
Use the function to parse JSON data into a Go object. This function takes a byte slice containing JSON data and a pointer to the target Go object, and maps the JSON data to the specified Go object.
package main import ( "encoding/json" "fmt" ) type Person struct { Name string `json:"name"` Age int `json:"age"` } func main() { jsonData := []byte(`{"name":"John", "age":30}`) var person Person err := (jsonData, &person) if err != nil { ("Error:", err) return } ("Name:", ) ("Age:", ) }
Serialization (convert Go objects to JSON)
Use the function to convert Go objects to JSON data. This function accepts a Go object and returns a JSON byte slice representing the object.
package main import ( "encoding/json" "fmt" ) type Person struct { Name string `json:"name"` Age int `json:"age"` } func main() { person := Person{ Name: "John", Age: 30, } jsonData, err := (person) if err != nil { ("Error:", err) return } (string(jsonData)) }
Handle nested structures and arrays
In Go, you can use structure nesting and slice/arrays to handle complex JSON data structures. By adding a json tag to the structure field, you can specify the key name in the JSON data.
package main import ( "encoding/json" "fmt" ) type Address struct { City string `json:"city"` State string `json:"state"` } type Person struct { Name string `json:"name"` Age int `json:"age"` Address []Address `json:"address"` } func main() { jsonData := []byte(`{"name":"John", "age":30, "address":[{"city":"New York","state":"NY"},{"city":"San Francisco","state":"CA"}]}`) var person Person err := (jsonData, &person) if err != nil { ("Error:", err) return } ("Name:", ) ("Age:", ) ("Address:", ) }
The above example shows how to process JSON data in Go. You can define your own data structure according to actual needs and use json tags for field mapping.
Read JSON files
- Use the os and io/iooutil packages to read the contents of a JSON file and store it as a byte slice.
- Use the function to parse the byte slice into a Go object.
package main import ( "encoding/json" "fmt" "io/ioutil" "os" ) type Person struct { Name string `json:"name"` Age int `json:"age"` } func main() { file, err := ("") if err != nil { ("Error:", err) return } defer () jsonData, err := (file) if err != nil { ("Error:", err) return } var person Person err = (jsonData, &person) if err != nil { ("Error:", err) return } ("Name:", ) ("Age:", ) }
In the above example, we open a JSON file named , and read its contents as byte slices. We then parse the byte slice into a Person object using .
Write to JSON file
- Use the os and io/iooutil packages to create or open the JSON file to be written.
- Use the function to convert a Go object into a JSON byte slice with indentation.
- Use the function to write a JSON byte slice to a JSON file.
package main import ( "encoding/json" "fmt" "io/ioutil" ) type Person struct { Name string `json:"name"` Age int `json:"age"` } func main() { person := Person{ Name: "John", Age: 30, } jsonData, err := (person, "", " ") if err != nil { ("Error:", err) return } err = ("", jsonData, 0644) if err != nil { ("Error:", err) return } ("JSON file created.") }
In the above example, we create a JSON file named , and convert the Person object into a JSON byte slice with indentation. We then write JSON byte slices to the file using .
This is the end of this article about the summary of commonly used json processing operations in Go language. For more related Go json processing content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!