栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Go语言

Golang 解析 json字符串 json数组

Go语言 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Golang 解析 json字符串 json数组

1. 解析json字符串到结构体

将json字符串解析到结构体,因为这里的 common_policy_config 是json数组,没办法用字段来表示,所以需要用 []interface{} 来存。

package main

import (
	"encoding/json"

	"github.com/beego/beego/v2/adapter/logs"
)

type Request_common_policy_config struct {
	Action               string        `json:"action"`
	RetCodo              int32         `json:"retCode"`
	Common_policy_config []interface{} `json:"common_policy_config"`
}

func main() {

	test := "{"action":"reponse_common_policy_config","retCode":0,"common_policy_config":[[3,15],"1234"]}"

	var config Request_common_policy_config

	err := json.Unmarshal([]byte(test), &config)

	if err != nil {
		logs.Info(err)
	}

	logs.Info(config)

	logs.Info("%T %v", config, config)
}

输出:

2. 解析json字符串

明确一点,当需要解析json的时候需要用到映射和空接口 map[string]interface{} 。
string 作为键的类型,interface{} 作为值类型,可以存放任何类型的对象。

package main

import (
	"encoding/json"
	"github.com/beego/beego/v2/adapter/logs"
)

func main() {

	test := "{"action":"reponse_common_policy_config","retCode":0,"common_policy_config":[[3,15],"1234"]}"

	var config map[string]interface{}
	err := json.Unmarshal([]byte(test), &config)

	if err != nil {
		logs.Info(err)
	}

	logs.Info(config)
	logs.Info("%T %v", config, config)

	logs.Info("%T", config["common_policy_config"])
}

输出

3. 解析json数组

当json字符串为json数组时,我们发现map[string]interface{} 并不好使了。因为golang 的映射类型必须要键值成对出现,键是值得索引,没有键就拿不到值。

当看了上面 common_policy_config 的输出,我们发现json数组的类型是 []interface{},即空接口数组,数组的元素可以是任意对象。

通过for循环,我们就能取出数组中的所有值了。

package main

import (
	"encoding/json"

	"github.com/beego/beego/v2/adapter/logs"
)

func main() {

	test := "[[3,15],"1234"]"

	var config []interface{}
	err := json.Unmarshal([]byte(test), &config)

	if err != nil {
		logs.Info(err)
	}

	logs.Info(config)
	logs.Info("%T %v", config, config)

	Print(config)
}

func Print(a interface{}) {
	b, ok := a.([]interface{})
	if !ok {
		logs.Info("%T %v", a, a)
	} else {
		for _, info := range b {
			Print(info)
		}
	}
}

输出:

当数组里面套的是json对象时,上面的[]interface{} 就不够用了。这时候需要用切片来存放数组元素,用 map[string]interface{} 来存在元素对象,即json字符串。需要用到类型是 []map[string]interface{} 。

package main

import (
	"encoding/json"

	"github.com/beego/beego/v2/adapter/logs"
)

type Request_common_policy_config struct {
	Action               string        `json:"action"`
	RetCodo              int32         `json:"retCode"`
	Common_policy_config []interface{} `json:"common_policy_config"`
}

func main() {

	test := "[{"action":"reponse_common_policy_config","retCode":0,"common_policy_config":[[3,15],"1234"]}]"

	var config []map[string]interface{}

	err := json.Unmarshal([]byte(test), &config)

	if err != nil {
		logs.Info(err)
	}

	logs.Info(config)

	logs.Info("%T %v", config, config)
}

输出:

不得不说,Golang处理json真的是太方便了。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/907190.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号