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

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

1. json.Unmarshal

函数func json.Unmarshal(data []byte, v any) error就是用来解析json编码data然后结果保存指针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("解码后的结果为:%v", *user1Struct)
}

程序运行后的结果如下

PS E:goland-workspaceGolangLearningCommonjson数据处理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用于从in中读取数据解析

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("数据的所有键值: %vn", viper.AllKeys())
	fmt.Printf("解析后的数据:%vn", viper.AllSettings())
	fmt.Printf("the type of "married" is %sn", reflect.TypeOf(viper.Get("married")))
	fmt.Printf("The name is %s and the country is %sn", viper.Get("name"), viper.Get("address.country"))
}

程序运行后的结果如下

PS E:goland-workspaceGolangLearningCommonjson数据处理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-workspaceGolangLearningCommonjson数据处理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)
}

程序运行后的结果如下

PS E:goland-workspaceGolangLearningCommonjson数据处理gojsonq> go run .main.go
The name is tian and the city is beijing

原文地址:https://blog.csdn.net/weixin_42216109/article/details/130254702

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_20590.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注