golang解析json數(shù)據(jù)的4種方法總結(jié)
前言
現(xiàn)在有一個(gè)json格式的字符串,應(yīng)該怎么解析呢,這里總結(jié)了以下4種方法
1. json.Unmarshal
函數(shù)func json.Unmarshal(data []byte, v any) error就是用來解析json編碼的data,然后將結(jié)果保存在指針v指向的值里
e.g.
package main
import (
"encoding/json"
"fmt"
)
type user struct {
Name string
Married bool
Address struct {
City string
Country string
}
}
func main() {
user1 := `{
"name": "tian",
"married": false,
"address": {
"city": "beijing",
"country": "China"
}
}`
user1Struct := &user{}
json.Unmarshal([]byte(user1), user1Struct)
fmt.Printf("解碼后的結(jié)果為:%v", *user1Struct)
}
- 首先根據(jù)json數(shù)據(jù)的格式定義
struct,用來保存解碼后的值。這里首先定義了一個(gè)user結(jié)構(gòu)體,然后通過json.Unmarshal進(jìn)行解碼 - 缺點(diǎn)很明顯,如果json數(shù)據(jù)很復(fù)雜,自定義的struct就跟著復(fù)雜。
程序運(yùn)行后的結(jié)果如下:
PS E:\goland-workspace\GolangLearning\Common\json數(shù)據(jù)處理\unmarshal> go run .\main.go
解碼后的結(jié)果為:{tian false {beijing China}}
2. viper.ReadConfig
使用go get -u github.com/spf13/viper 進(jìn)行下載
函數(shù)func viper.ReadConfig(in io.Reader) error用于從in中讀取數(shù)據(jù)并解析
e.g.
package main
import (
"fmt"
"strings"
"github.com/spf13/viper"
)
func main() {
user1 := `{
"name": "tian",
"married": false,
"address": {
"city": "beijing",
"country": "China"
}
}`
// 指定配置的類型為json
viper.SetConfigType("json")
// 讀取數(shù)據(jù)
if err := viper.ReadConfig(strings.NewReader(user1)); err != nil {
fmt.Println(err)
}
fmt.Printf("數(shù)據(jù)的所有鍵值: %v\n", viper.AllKeys())
fmt.Printf("解析后的數(shù)據(jù):%v\n", viper.AllSettings())
fmt.Printf("the type of \"married\" is %s\n", reflect.TypeOf(viper.Get("married")))
fmt.Printf("The name is %s and the country is %s\n", viper.Get("name"), viper.Get("address.country"))
}
首先要通過viper.SetConfigType("json")指定要解析數(shù)據(jù)的格式,否則即使viper.ReadConfig返回值沒有報(bào)錯(cuò),也得不到解析后的結(jié)果。可以查看https://github.com/spf13/viper/issues/316
方法viper.Get(),viper.GetString(),viper.GetBool()等等可以方便獲取鍵值,同時(shí)對(duì)于鍵值的類型也能很好的判斷
程序運(yùn)行后的結(jié)果如下:
PS E:\goland-workspace\GolangLearning\Common\json數(shù)據(jù)處理\viper> go run .\main.go
數(shù)據(jù)的所有鍵值: [address.city address.country name married]
解析后的數(shù)據(jù):map[address:map[city:beijing country:China] married:false name:tian]
the type of "married" is bool
The name is tian and the country is China
3. simplejson.NewJson
使用go get -u "github.com/bitly/go-simplejson"進(jìn)行下載
e.g.
package main
import (
"fmt"
"github.com/bitly/go-simplejson"
)
func main() {
user1 := `{
"name": "tian",
"married": false,
"address": {
"city": "beijing",
"country": "China"
}
}`
user1json, err := simplejson.NewJson([]byte(user1))
if err != nil {
fmt.Println(err)
}
name1, _ := user1json.Get("name").String()
city1, _ := user1json.Get("address").Get("city").String()
fmt.Printf("The name is %s and the city is %s", name1, city1)
}
程序運(yùn)行后的結(jié)果如下:
PS E:\goland-workspace\GolangLearning\Common\json數(shù)據(jù)處理\simpleJson> go run .\main.go
The name is tian and the city is beijing
4. gojsonq.New().FromString()
使用go get -u github.com/thedevsaddam/gojsonq安裝
e.g.
package main
import (
"fmt"
"github.com/thedevsaddam/gojsonq/v2"
)
func main() {
user1 := `{
"name": "tian",
"married": false,
"address": {
"city": "beijing",
"country": "China"
}
}`
user1json := gojsonq.New().FromString(user1)
name1 := user1json.Find("name").(string)
user1json.Reset()
city1 := user1json.Find("address.city")
fmt.Printf("The name is %s and the city is %v", name1, city1)
}
在第一次查詢name之后,手動(dòng)調(diào)用了一次Reset()方法。因?yàn)镴SONQ對(duì)象在調(diào)用Find方法時(shí),內(nèi)部會(huì)記錄當(dāng)前的節(jié)點(diǎn),下一個(gè)查詢會(huì)從上次查找的節(jié)點(diǎn)開始
程序運(yùn)行后的結(jié)果如下:
PS E:\goland-workspace\GolangLearning\Common\json數(shù)據(jù)處理\gojsonq> go run .\main.go
The name is tian and the city is beijing
總結(jié)
到此這篇關(guān)于golang解析json數(shù)據(jù)的4種方法的文章就介紹到這了,更多相關(guān)golang解析json數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Go與Rust高性能解析JSON實(shí)現(xiàn)方法示例
- Golang解析JSON遇到的坑及解決方法
- Go語言學(xué)習(xí)之JSON編碼解析與使用
- Go語言實(shí)現(xiàn)JSON解析的神器詳解
- 一文帶你了解Go語言如何解析JSON
- Go語言JSON解析器gjson使用方法詳解
- Golang實(shí)現(xiàn)解析JSON的三種方法總結(jié)
- golang生成JSON以及解析JSON
- Go?語言?json解析框架與?gjson?詳解
- go語言用八百行代碼實(shí)現(xiàn)一個(gè)JSON解析器
- Go語言實(shí)現(xiàn)JSON解析的方法詳解
- GO中Json解析的幾種方式
相關(guān)文章
利用golang實(shí)現(xiàn)pdf中自動(dòng)換行的表格
這篇文章主要給大家介紹了如何利用golang實(shí)現(xiàn)pdf中自動(dòng)換行的表格,文中通過代碼示例給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-02-02
基于Go語言實(shí)現(xiàn)應(yīng)用IP防火墻
在公司里面經(jīng)常會(huì)聽到某應(yīng)用有安全漏洞問題,沒有做安全加固,IP防火墻就是一個(gè)典型的安全加固解決方案,下面我們就來學(xué)習(xí)一下如何使用go語言實(shí)現(xiàn)IP防火墻吧2023-11-11
詳解如何使用Golang操作MongoDB數(shù)據(jù)庫
在現(xiàn)代開發(fā)中,數(shù)據(jù)存儲(chǔ)是一個(gè)至關(guān)重要的環(huán)節(jié),MongoDB作為一種NoSQL數(shù)據(jù)庫,提供了強(qiáng)大的功能和靈活的數(shù)據(jù)模型,與Golang的高性能和并發(fā)性能非常契合,本文將探討Golang與MongoDB的完美組合,介紹如何使用Golang操作MongoDB數(shù)據(jù)庫,需要的朋友可以參考下2023-11-11
Go項(xiàng)目配置管理神器之viper的介紹與使用詳解
viper是一個(gè)完整的?Go應(yīng)用程序的配置解決方案,它被設(shè)計(jì)為在應(yīng)用程序中工作,并能處理所有類型的配置需求和格式,下面這篇文章主要給大家介紹了關(guān)于Go項(xiàng)目配置管理神器之viper的介紹與使用,需要的朋友可以參考下2023-02-02

