先看例子, json文件中有些元素不是我们想要的,在反序列化时可以当它们不存在,下面例子抛弃了 aaa、ccc这两节。
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"],
"ccc":"212",
"Publisher":"qinghua",
"IsPublished":true,
"Price":99,
"aaa":"21"
}`)
//先创建一个目标类型的实例对象,用于存放解码后的值
var book Book
err := json.Unmarshal(b, &book)
if err != nil {
fmt.Println("error in translating,", err.Error())
return
}
fmt.Println(book.Author)
}