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

common_policy_config[]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字符串

map[string]interface{}stringinterface{}
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数组

map[string]interface{}
common_policy_config[]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)
		}
	}
}

输出:
在这里插入图片描述

[]interface{}map[string]interface{}[]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真的是太方便了。