Go語言利用Unmarshal解析json字符串的實(shí)現(xiàn)
簡(jiǎ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}]
簡(jiǎn)單進(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}
還是之前的例子:
解析這樣的一個(gè)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"` ?//指針,指向引用對(duì)象;如果不用指針,只是值復(fù)制
?? ?SecondFruit *Fruit `json:"second fruit"` //指針,指向引用對(duì)象;如果不用指針,只是值復(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)
}運(yùn)行結(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字符串的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Go Unmarshal解析json字符串內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
golang中sync.Once只執(zhí)行一次的原理解析
在某些場(chǎng)景下,我們希望某個(gè)操作或者函數(shù)僅被執(zhí)行一次,比如單例模式的初始化,一些資源配置的加載等,golang中的sync.Once就實(shí)現(xiàn)了這個(gè)功能,本文就和大家一起解析sync.Once只執(zhí)行一次的原理,需要的朋友可以參考下2023-09-09
Goland 斷點(diǎn)調(diào)試Debug的操作
這篇文章主要介紹了Goland 斷點(diǎn)調(diào)試Debug的操作方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-04-04
golang使用http client發(fā)起get和post請(qǐng)求示例
這篇文章主要介紹了golang使用http client發(fā)起get和post請(qǐng)求示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
Go1.21新增內(nèi)置函數(shù)(built-in?functions)詳解
Go?1.21新增的內(nèi)置函數(shù)分別是?min、max?和?clear,這篇文章主要帶大家一起了解一下這幾個(gè)函數(shù)的用途和使用示例,感興趣的小伙伴可以學(xué)習(xí)一下2023-08-08
詳解Golang中Context的三個(gè)常見應(yīng)用場(chǎng)景
Golang?context主要用于定義超時(shí)取消,取消后續(xù)操作,在不同操作中傳遞值。本文通過簡(jiǎn)單易懂的示例進(jìn)行說明,感興趣的可以了解一下2022-12-12
教你利用Golang可選參數(shù)實(shí)現(xiàn)可選模式
本文討論Golang函數(shù)可選參數(shù)及函數(shù)類型,以及如何利用可選函數(shù)類型實(shí)現(xiàn)可選模式。同時(shí)通過構(gòu)造函數(shù)作為示例,實(shí)現(xiàn)強(qiáng)大帶可選參數(shù)的構(gòu)造函數(shù),讓代碼更直觀、靈活、支持?jǐn)U展2023-01-01
GO語言導(dǎo)入自己寫的包(同級(jí)目錄和不同目錄)
本文介紹了如何在Go語言項(xiàng)目中導(dǎo)入同級(jí)目錄和不同目錄的包,詳細(xì)解釋了創(chuàng)建文件結(jié)構(gòu)、編寫主函數(shù)、同級(jí)目錄和不同目錄方法的調(diào)用,適合初學(xué)者參考,幫助理解Go項(xiàng)目的基本構(gòu)建和包管理2024-09-09

