Golang 提供了多个 API 来处理 JSON,包括使用encoding/json包的内置和自定义数据类型。为了解析 JSON,我们使用包 encoding/json 中的Unmarshal()函数将数据从 JSON 解包或解码为结构。
Syntax: func Unmarshal(data []byte, v interface{}) error
Unmarshal 解析 JSON 编码的数据并将结果存储在 v 指向的值中。
注意:如果 v 为 nil 或不是指针,则 Unmarshal 返回InvalidUnmarshalError。
示例 1:
// Golang program to illustrate the
// concept of parsing json to struct
package main
import (
"encoding/json"
"fmt"
)
// declaring a struct
type Country struct {
// defining struct variables
Name string
Capital string
Continent string
}
// main function
func main() {
// defining a struct instance
var country1 Country
// data in JSON format which
// is to be decoded
Data := []byte(`{
"Name": "India",
"Capital": "New Delhi",
"Continent": "Asia"
}`)
// decoding country1 struct
// from json format
err := json.Unmarshal(Data, &country1)
if err != nil {
// if error is not nil
// print error
fmt.Println(err)
}
// printing details of
// decoded data
fmt.Println("Struct is:", country1)
fmt.Printf("%s's capital is %s and it is in %s.\n", country1.Name,
country1.Capital, country1.Continent)
}
输出:
Struct is: {India New Delhi Asia}
India's capital is New Delhi and it is in Asia.
示例 2:
// Golang program to illustrate the
// concept of parsing JSON to an array
package main
import (
"encoding/json"
"fmt"
)
// declaring a struct
type Country struct {
// defining struct variables
Name string
Capital string
Continent string
}
// main function
func main() {
// defining a struct instance
var country []Country
// JSON array to be decoded
// to an array in golang
Data := []byte(`
[
{"Name": "Japan", "Capital": "Tokyo", "Continent": "Asia"},
{"Name": "Germany", "Capital": "Berlin", "Continent": "Europe"},
{"Name": "Greece", "Capital": "Athens", "Continent": "Europe"},
{"Name": "Israel", "Capital": "Jerusalem", "Continent": "Asia"}
]`)
// decoding JSON array to
// the country array
err := json.Unmarshal(Data, &country)
if err != nil {
// if error is not nil
// print error
fmt.Println(err)
}
// printing decoded array
// values one by one
for i := range country {
fmt.Println(country[i].Name + " - " + country[i].Capital +
" - " + country[i].Continent)
}
}
输出:
Japan - Tokyo - Asia
Germany - Berlin - Europe
Greece - Athens - Europe
Israel - Jerusalem - Asia