golang学习笔记 ---解析(map[string]interface{})数据格式

interface转其他类型 
有时候返回值是interface类型的,直接赋值是无法转化的

package main

import (
	"fmt"
)

func main() {

	var a interface{}
	var b string
	a = "123"
	//b = a //cannot use a (type interface {}) as type string in assignment: need type assertion
	b = a.(string)
	fmt.Println(b)
}

  

输出:

123

 

通过a.(string) 转化为string,通过a.(int)转化为类型。 

package main

import (
	"fmt"
)

func main() {

	var a interface{}
	var b string
	var c int
	a = "123"
	//b = a //cannot use a (type interface {}) as type string in assignment: need type assertion
	b = a.(string)
	fmt.Println(b)
	a = 124
	c = a.(int)
	fmt.Println(c)

}

可以通过a.(type)来判断a可以转为什么类型。

注意事项

  • map记得分配内存
  • 解析出来的int类型会变成float64类型
  • 注意判断不为nil后再转换类型
package main

import (
	"encoding/json"
	"fmt"
)

func main() {
	//1.准备一段json
	b := []byte(`{"Name":"sary","Age":18}`)
	//2.声明空接口
	var i interface{}
	//3.解析
	err := json.Unmarshal(b, &i)
	if err != nil {
		fmt.Println("json err:", err)
	}
	//自动转map
	fmt.Println(i)
	//4.使用interface的json,可以判断类型
	m := i.(map[string]interface{})
	for k, v := range m {
		switch value := v.(type) {
		case nil:
			fmt.Println(k, "is nil", "null")
		case string:
			fmt.Println(k, "is string", value)
		case int:
			fmt.Println(k, "is int", value)
		case float64:
			fmt.Println(k, "is float64", value)
		case []interface{}:
			fmt.Println(k, "is an array:")
			for i, u := range value {
				fmt.Println(i, u)
			}
		case map[string]interface{}:
			fmt.Println(k, "is an map:")
		default:
			fmt.Println(k, "is unknown type", fmt.Sprintf("%T", v))
		}
	}

}

  

输出:

map[Age:18 Name:sary]

Name is string sary

Age is float64 18

package main

import (
	"encoding/json"
	"fmt"
)

func main() {
	var m map[string]interface{}     //声明变量,不分配内存
	m = make(map[string]interface{}) //必可不少,分配内存
	m["name"] = "sary"
	var age int = 18
	m["age"] = age
	m["addr"] = "China"
	print_map(m)
	fmt.Println()

	data, err := json.Marshal(m)
	if err != nil {
		fmt.Println("err:", err)
	} else {
		fmt.Println("data:", data)

	}

	m1 := make(map[string]interface{})
	err = json.Unmarshal(data, &m1)
	if err != nil {
		fmt.Println("err:", err)
	} else {
		print_map(m1)

	}

	fmt.Println()
	value, ok := m1["key1"]
	if ok {
		fmt.Printf(value.(string))
	} else {
		fmt.Println("key1 不存在")
	}

}

//解析 map[string]interface{} 数据格式
func print_map(m map[string]interface{}) {

	fmt.Println("enter print_map########### ")
	for k, v := range m {
		switch value := v.(type) {
		case nil:
			fmt.Println(k, "is nil", "null")
		case string:
			fmt.Println(k, "is string", value)
		case int:
			fmt.Println(k, "is int", value)
		case float64:
			fmt.Println(k, "is float64", value)
		case []interface{}:
			fmt.Println(k, "is an array:")
			for i, u := range value {
				fmt.Println(i, u)
			}
		case map[string]interface{}:
			fmt.Println(k, "is an map:")
			print_map(value)
		default:
			fmt.Println(k, "is unknown type", fmt.Sprintf("%T", v))
		}
	}
	fmt.Println("out print_map ########### ")
}

  

输出:

enter print_map###########

name is string sary

age is int 18

addr is string China

out print_map ###########

 

data: [123 34 97 100 100 114 34 58 34 67 104 105 110 97 34 44 34 97 103 101 34 58 49 56 44 34 110 97 109 101 34 58 34 115 97 114 121 34 125]

enter print_map###########

addr is string China

age is float64 18

name is string sary

out print_map ###########

 

key1 不存在

 

posted on 2021-10-20 13:23  清明-心若淡定  阅读(10488)  评论(0编辑  收藏  举报