Go語言利用Unmarshal解析json字符串的實現(xiàn)
簡單的解析例子:
首先還是從官方文檔中的例子:
package main
import (
?? ?"fmt"
?? ?"encoding/json"
)
type Animal struct {
? ? Name ?string
? ? Order string
}
func main() {
?? ?var jsonBlob = []byte(`[
?? ??? ?{"Name": "Platypus", "Order": "Monotremata"},
?? ??? ?{"Name": "Quoll", ? ?"Order": "Dasyuromorphia"}
?? ?]`)
?? ?var animals []Animal
?? ?
?? ?err := json.Unmarshal(jsonBlob, &animals)
?? ?if err != nil {
?? ? ? ?fmt.Println("error:", err)
?? ?}
?? ?fmt.Printf("%+v", animals)
}輸出:
[{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]
簡單進(jìn)行修改,修改為:
package main
import (
?? ?"fmt"
?? ?"encoding/json"
)
type Animal struct {
? ? Name ?string
? ? Order string
}
func main() {
?? ?var jsonBlob = []byte(`{"Name": "Platypus", "Order": "Monotremata"}`)
?? ?var animals Animal
?? ?err := json.Unmarshal(jsonBlob, &animals)
?? ?if err != nil {
?? ? ? ?fmt.Println("error:", err)
?? ?}
?? ?fmt.Printf("%+v", animals)
}輸出:
{Name:Platypus Order:Monotremata}
還是之前的例子:
解析這樣的一個json字符串:
{
? ? "first fruit":
? ? {
? ? ? ? "describe":"an apple",
? ? ? ? "icon":"appleIcon",
? ? ? ? "name":"apple"
? ? },
? ? "second fruit":
? ? {
? ? ? ? "describe":"an orange",
? ? ? ? "icon":"orangeIcon",
? ? ? ? "name":"orange"
? ? },
? ? "three fruit array":
? ? [
? ? ? ? "eat 0",
? ? ? ? "eat 1",
? ? ? ? "eat 2",
? ? ? ? "eat 3",
? ? ? ? "eat 4"
? ? ]
}go代碼:
package main
import (
?? ?"fmt"
?? ?"encoding/json"
)
type Fruit struct {
?? ?Describe string `json:"describe"`
?? ?Icon ? ? string `json:"icon"`
?? ?Name ? ? string `json:"name"`
}
type FruitGroup struct {
?? ?FirstFruit ?*Fruit `json:"first fruit"` ?//指針,指向引用對象;如果不用指針,只是值復(fù)制
?? ?SecondFruit *Fruit `json:"second fruit"` //指針,指向引用對象;如果不用指針,只是值復(fù)制
?? ?THreeFruitArray []string `json:"three fruit array"`
}
func main() {
?? ?var jsonBlob = []byte(`{
? ? "first fruit": {
? ? ? ? "describe": "an apple",
? ? ? ? "icon": "appleIcon",
? ? ? ? "name": "apple"
? ? },
? ? "second fruit": {
? ? ? ? "describe": "an orange",
? ? ? ? "icon": "appleIcon",
? ? ? ? "name": "orange"
? ? },
? ? "three fruit array": [
? ? ? ? "eat 0",
? ? ? ? "eat 1",
? ? ? ? "eat 2",
? ? ? ? "eat 3"
? ? ]}`)
?? ?var fruitGroup FruitGroup
?? ?
?? ?err := json.Unmarshal(jsonBlob, &fruitGroup)
?? ?if err != nil {
?? ? ? ?fmt.Println("error:", err)
?? ?}
?? ?fmt.Printf("%+v\n", fruitGroup)
?? ?fmt.Printf("%+v\n", fruitGroup.FirstFruit)
?? ?fmt.Printf("%+v\n", fruitGroup.SecondFruit)
}運行結(jié)果:
{FirstFruit:0xc00006c5a0 SecondFruit:0xc00006c5d0 THreeFruitArray:[eat 0 eat 1 eat 2 eat 3]}
&{Describe:an apple Icon:appleIcon Name:apple}
&{Describe:an orange Icon:appleIcon Name:orange}
到此這篇關(guān)于Go語言利用Unmarshal解析json字符串的實現(xiàn)的文章就介紹到這了,更多相關(guān)Go Unmarshal解析json字符串內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Go調(diào)用C++動態(tài)庫實現(xiàn)車牌識別的示例代碼
本文主要介紹了如何利用C++中Opencv、TensorRT等庫編譯出動態(tài)庫供Go調(diào)用,再寫個簡單的api對上傳的車輛圖片進(jìn)行車牌識別,文中通過代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
Golang中類型轉(zhuǎn)換利器cast庫的用法詳解
cast庫是一個簡潔而強大的第三方庫,它的主要功能是實現(xiàn)類型之間的安全轉(zhuǎn)換,而在Golang開發(fā)中,類型轉(zhuǎn)換是一個常見且不可避免的過程,下面我們就來看看cast庫在Golang中的具體應(yīng)用吧2024-11-11
詳解Go是如何優(yōu)雅的進(jìn)行內(nèi)存管理
Go語言拋棄C/C++中的開發(fā)者管理內(nèi)存的方式,實現(xiàn)了主動申請與主動釋放管理,增加了逃逸分析和垃圾回收,將開發(fā)者從內(nèi)存管理中釋放出來,作為進(jìn)階的Go開發(fā),了解掌握Go的內(nèi)存管理還是很有必要的2023-09-09
go中利用reflect實現(xiàn)json序列化的示例代碼
和Java語言一樣,Go也實現(xiàn)運行時反射,這為我們提供一種可以在運行時操作任意類型對象的能力,本文給大家介紹了在go中如何利用reflect實現(xiàn)json序列化,需要的朋友可以參考下2024-03-03

