zoukankan      html  css  js  c++  java
  • go语言的json

    简介

    json 中提供的处理 json 的标准包是 encoding/json,主要使用的是以下两个方法:

    // 序列化
    func Marshal(v interface{}) ([]byte, error)
    
    // 反序列化
    func Unmarshal(data []byte, v interface{}) error
    

    1、编码json

    可以把一个结构体编码为json,也可以把一个map编码为json

    package main
    
    import (
    	"encoding/json"
    	"fmt"
    )
    
    //编码json,使用json.Marshal()函数可以对一组数据进行json格式的编码
    
    
    //定义 一个结构体
    type Test39Person struct {
    	//这里的属性必须是大写开头
    	Test1 string
    	Test2 string
    
    }
    
    //看下后面的打印就知道什么意思了,实现小写也可以了,这里也可以搞一个匿名变量,不会打印,用-
    type Test39Person1 struct {
    	//这里的属性必须是大写开头
    	Test1 string `json:"-"`
    	Test2 string `json:"hob"`
    
    }
    
    
    
    
    
    func main() {
    	//1、通过结构体生成json
    
    	p := Test39Person{ Test1:"abc", Test2:"ate"}
    
    	p2 := Test39Person1{ Test1:"abc", Test2:"ate"}
    
    	fmt.Println(p)
    	//1、生成json文本
    	b,e := json.Marshal(p)
    	if e != nil {
    		fmt.Println(e)
    	}
    	fmt.Println(string(b))
    	//{"Test1":"abc","Test2":"ate"}
    
    
    	//2、生成格式化json
    	//prefix是前缀的意思,indent
    	c,e := json.MarshalIndent(p,"*","     ")
    	fmt.Println(string(c))
    
    	//{
    	//	*     "Test1": "abc",
    	//	*     "Test2": "ate"
    	//	*}
    
    
    	c,e = json.MarshalIndent(p2,"*","     ")
    	fmt.Println(string(c))
    	//{
    	//	*     "hob": "ate"
    	//	*}
    
    	//3、通过map生成json
    //interface这个表示任意类型
    	nnp := make(map[string]interface{})
    	nnp["name"] = "ccc"
    	nnp["age"] = 12
    	nnp["hob"] = true
    
    	f,e := json.Marshal(nnp)
    	if e != nil {
    		fmt.Println(e)
    	}else {
    		fmt.Println(string(f))
    		//{"age":12,"hob":true,"name":"ccc"}
    	}
    
    
    }
    

      

    2、解码json

    可以解码到结构体,也可以解码到接口

    package main
    
    import (
    	"encoding/json"
    	"fmt"
    	"reflect"
    )
    
    //解码json
    type Test40Person struct {
    	//这里的属性必须是大写开头
    	Test1 string
    	Test2 string
    
    }
    func main() {
    	//准备一段json
    	b := []byte(`{"Test1":"abc","Test2":"ate"}`)
    
    	//把json解析到结构体
    	var t Test40Person
    
    	e := json.Unmarshal(b,&t)
    	if e != nil {
    		fmt.Println(e)
    	}
    	fmt.Println(t,reflect.TypeOf(t))
    	//{abc ate} main.Test40Person
    
    
    	//解析到接口,解析出来的数据是map
    	var i interface{}
    
    	err := json.Unmarshal(b,&i)
    	if err != nil {
    		fmt.Println(err)
    	}
    	fmt.Println(i,reflect.TypeOf(i))
    	//map[Test1:abc Test2:ate] map[string]interface {}
    
    
    
    	//调用interface的json,可以判断类型
    	m := i.(map[string]interface{})
    	for k,v := range m {
    		switch vv := v.(type) {
    		case string:
    			fmt.Println(k,"是string类型",vv)
    		case int:
    			fmt.Println(k,"是int类型",vv)
    		default:
    			fmt.Println(k,"是其他类型",vv)
    		}
    
    	}
    
    }
    

      

    1、序列化为结构体为json,采用json.Marshal()方法

    package main
    
    import (
    	"encoding/json"
    	"fmt"
    	"reflect"
    )
    
    //定义一个结构体
    type Test5Person struct {
    	Test1 string
    	Test2 byte
    	Test3 bool
    	Test4 int
    }
    
    
    
    func main()  {
    	//将一个结构体序列化为json
    	t5_1 := Test5Person{Test1:"aaa",Test2:'b',Test3:false,Test4:12}
    	fmt.Println(t5_1)
    	//{aaa 98 false 12}
    	d,e := json.Marshal(t5_1)
    	if e != nil {
    		fmt.Println(e)
    	}else {
    		fmt.Println(string(d),reflect.TypeOf(d))
    		//{"Test1":"aaa","Test2":98,"Test3":false,"Test4":12} []uint8
    	}
    }
    

    2、序列化map为json,采用json.Marshal()方法

    package main
    
    import (
    	"encoding/json"
    	"fmt"
    )
    
    type Test6Map1 map [string] interface{}
    
    func main()  {
    	var t6_1 = Test6Map1{"test1":"test111","test2":"test222","test3":"test333"}
    
    
    
    	d,e := json.Marshal(t6_1)
    	if e != nil {
    		fmt.Println(e)
    	}else {
    		fmt.Println(string(d))
    	}
    
    
    	var t6_2 = make(map[int]interface{},10)
    	t6_2[1] = "11111111"
    	t6_2[2] = 22222222
    	t6_2[3] = 'b'
    	t6_2[4] = true
    	t6_2[5] = 3.14
    	t6_2[6] = false
    
    	d,e = json.Marshal(t6_2)
    	if e != nil {
    		fmt.Println(e)
    	}else {
    		fmt.Println(string(d))
    	}
    
    	//{"1":"11111111","2":22222222,"3":98,"4":true,"5":3.14,"6":false}
    
    }
    

    3、反序列化,采用json.Unmarshal(b,&i)方法

    package main
    
    import (
    	"encoding/json"
    	"fmt"
    )
    
    type Test7Person1 struct {
    	Name1 string
    	Name2 int
    	Name3 byte
    	Name4 bool
    	Name5 float32
    	//Name6 bool
    
    }
    
    func main()  {
    
    	//反序列化到结构体,只会反序列化结构体中有的字段,如果json中有字段1,而在结构体中没有字段1,则反序列化就不会有字段1
    	var b = []byte(`{"Name1":"11111111","Name2":22,"Name3":98,"Name4":true,"Name5":3.14,"Name6":false}`)
    	//var b = []byte(`{"Name1":"test1","Name2":"test2","Name3":"test3"}`)
    	var t7 Test7Person1
    
    	e := json.Unmarshal(b,&t7)
    
    	if e != nil {
    		fmt.Println(e)
    	}else {
    		fmt.Println(t7)
    		//{11111111 22 98 true 3.14}
    	}
    
    
    	//反序列化到接口
    	var i interface{}
    
    	e1 := json.Unmarshal(b,&i)
    
    	if e1 != nil {
    		fmt.Println(e)
    	}else {
    		fmt.Println(i)
    		//map[Name1:11111111 Name2:22 Name3:98 Name4:true Name5:3.14 Name6:false]
    	}
    
    }
    
  • 相关阅读:
    SLA合规率是什么? SLA合规率实践分析
    如何借助Site24x7提高Azure VM的灵活性和效率?
    如何避免LLMNR / NBT-NS欺骗攻击?
    ITSM整治混乱秩序 | 看知名研究所如何解决帮助台管理
    如何使用Applications Manager轻松监控Nginx?
    vue中让input框自动聚焦
    pc端手机号填写与验证信息的样式
    vuex里面的this.$store.dispatch 和 this.$store.commit用法以及区别
    Vue 的状态管理工具 Vuex(Vuex的安装与使用)
    详解Vue的钩子函数(路由导航守卫、keep-alive、生命周期钩子)
  • 原文地址:https://www.cnblogs.com/bainianminguo/p/10903099.html
Copyright © 2011-2022 走看看