json.Marshal()
解析简单JSON
package main
import (
"fmt"
"encoding/json"
"time"
)
func main() {
type FruitBasket struct {
Name string
Fruit []string
Id int64 `json:"ref"`// 声明对应的json key
Created time.Time
}
jsonData := []byte(`
{
"Name": "Standard",
"Fruit": [
"Apple",
"Banana",
"Orange"
],
"ref": 999,
"Created": "2018-04-09T23:00:00Z"
}`)
var basket FruitBasket
err := json.Unmarshal(jsonData, &basket)
if err != nil {
fmt.Println(err)
}
fmt.Println(basket.Name, basket.Fruit, basket.Id)
fmt.Println(basket.Created)
}
json.UnMarshal()c := []byte(s)
解析内嵌对象的JSON
"Fruit" : {"Name", "Apple", "PriceTag": "$1"}
jsonData := []byte(`
{
"Name": "Standard",
"Fruit" : {"Name": "Apple", "PriceTag": "$1"},
"ref": 999,
"Created": "2018-04-09T23:00:00Z"
}`)
那么结构体类型应该这么声明
type Fruit struct {
Name string `json":Name"`
PriceTag string `json:"PriceTag"`
}
type FruitBasket struct {
Name string
Fruit Fruit
Id int64 `json:"ref"`// 声明对应的json key
Created time.Time
}
解析内嵌对象数组的JSON(Embed Array of Object)
如果上面JOSN对象里的Fruit值现在变成了
"Fruit" : [
{
"Name": "Apple",
"PriceTag": "$1"
},
{
"Name": "Pear",
"PriceTag": "$1.5"
}
]
[]Fruit
type Fruit struct {
Name string `json:"Name"`
PriceTag string `json:"PriceTag"`
}
type FruitBasket struct {
Name string
Fruit []Fruit
Id int64 `json:"ref"`// 声明对应的json key
Created time.Time
}
解析具有动态Key的对象(Parse a JSON object with dynamic key)
下面再做一下复杂的变通,如果把上面的对象数组变为Key为水果ID的对象(object of object)比如
"Fruit" : {
"1": {
"Name": "Apple",
"PriceTag": "$1"
},
"2": {
"Name": "Pear",
"PriceTag": "$1.5"
}
}
每个Key的名字在声明结构体的时候是不知道值的,这样该怎么声明呢,答案是把Fruit字段的类型声明为一个key为string类型值为Fruit类型的map
type Fruit struct {
Name string `json:"Name"`
PriceTag string `json:"PriceTag"`
}
type FruitBasket struct {
Name string
Fruit map[string]Fruit
Id int64 `json:"ref"`// 声明对应的json key
Created time.Time
}
示例代码
package main
import (
"fmt"
"encoding/json"
"time"
)
func main() {
type Fruit struct {
Name string `json:"Name"`
PriceTag string `json:"PriceTag"`
}
type FruitBasket struct {
Name string
Fruit map[string]Fruit
Id int64 `json:"ref"`// 声明对应的json key
Created time.Time
}
jsonData := []byte(`
{
"Name": "Standard",
"Fruit" : {
"1": {
"Name": "Apple",
"PriceTag": "$1"
},
"2": {
"Name": "Pear",
"PriceTag": "$1.5"
}
},
"ref": 999,
"Created": "2018-04-09T23:00:00Z"
}`)
var basket FruitBasket
err := json.Unmarshal(jsonData, &basket)
if err != nil {
fmt.Println(err)
}
for _, item := range basket.Fruit {
fmt.Println(item.Name, item.PriceTag)
}
}
解析包含任意层级的数组和对象的JSON数据(arbitrary arrays and objects)
encode\jsonencoding\json
- map[string]interface{} 存储JOSN对象
- []interface 存储JOSN数组
json.Unmarshl
示例代码:
jsonData := []byte(`{"Name":"Eve","Age":6,"Parents":["Alice","Bob"]}`)
var v interface{}
json.Unmarshal(jsonData, &v)
data := v.(map[string]interface{})
for k, v := range data {
switch v := v.(type) {
case string:
fmt.Println(k, v, "(string)")
case float64:
fmt.Println(k, v, "(float64)")
case []interface{}:
fmt.Println(k, "(array):")
for i, u := range v {
fmt.Println(" ", i, u)
}
default:
fmt.Println(k, v, "(unknown)")
}
}
虽然将JSON数据存储到空接口类型的值中可以用来解析任意结构的JSON数据,但是在实际应用中发现还是有不可控的地方,比如将数字字符串的值转换成了float类型的值,所以经常会在运行时报类型断言的错误,所以在JSON结构确定的情况下还是优先使用结构体类型声明,将JSON数据到结构体中的方式来解析JSON。