现在有一个json格式的字符串,应该怎么解析呢,这里总结了以下4种方法

1. json.Unmarshal
func json.Unmarshal(data []byte, v any) error

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("解码后的结果为:%v", *user1Struct)
}
structjson.Unmarshal

程序运行后的结果如下:

PS E:\goland-workspace\GolangLearning\Common\json数据处理\unmarshal> go run .\main.go
解码后的结果为:{tian false {beijing China}}
2. viper.ReadConfig
go get -u github.com/spf13/viper 
func viper.ReadConfig(in io.Reader) error

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")
	// 读取数据
	if err := viper.ReadConfig(strings.NewReader(user1)); err != nil {
		fmt.Println(err)
	}
	fmt.Printf("数据的所有键值: %v\n", viper.AllKeys())
	fmt.Printf("解析后的数据:%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")viper.ReadConfigviper.Get(),viper.GetString(),viper.GetBool()

程序运行后的结果如下:

PS E:\goland-workspace\GolangLearning\Common\json数据处理\viper> go run .\main.go
数据的所有键值: [address.city address.country name married]
解析后的数据: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"

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)
}

程序运行后的结果如下:

PS E:\goland-workspace\GolangLearning\Common\json数据处理\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)
}
Reset()Find

程序运行后的结果如下:

PS E:\goland-workspace\GolangLearning\Common\json数据处理\gojsonq> go run .\main.go
The name is tian and the city is beijing