Golang实现结构体和Json格式数据之间的互相转换

作者:

文章目录
  • 本节主要学习Golang结构体和JSON序列化数据的转换命令。
  • 目录
    • 摘要
    • 1. 结构体到json格式
      • 1.1 简单转换
      • 1.2 递归转换
    • 2. json格式到结构体
      • 2.1 简单转换
      • 2.2 嵌套JSON格式数据转换

    本节主要学习Golang结构体和JSON序列化数据的转换命令。

    Golang结构体转换成JSON格式数据,主要在结构体的相关字段中加入json : "keyword"字段。具体做法如下:

    type Structname struct{
    	feild1 Type1 `json:"keyword1"`
    	feild2 Type2 `json:"keyword2"`
    }
    

    相关具体实例如下:

    package message
    
    import (
    	"encoding/json"
    	"log"
    	"testing"
    )
    
    type Information struct{
    	Name string `json:"name"`
    	Addr string `json:"addr"`
    }
    
    func TestStructure(t *testing.T){
    	var inf Information
    	inf.Name="Alice"
    	inf.Addr="Green Street"
    	data,err:=json.Marshal(inf)
    	if err!=nil{
    		panic(err)
    	}
    	log.Println(string(data))
    }
    

    为了转换一个嵌套结构体为JSON格式文件,首先在需要转换的结构体中构建json:"keyword"字段,其次,构建一个嵌套的Golang结构体,最后利用Json.Marshal函数进行转换。

    package message
    
    import (
    	"encoding/json"
    	"errors"
    	"log"
    	"testing"
    )
    
    type Employee struct {
    	Position string `json:"position"`
    	Name     Name   `json:"name"`
    }
    
    type Name struct {
    	FirstName string `json:"firstname"`
    	Surname   string `json:"surname"`
    }
    
    func TestStructure(t *testing.T){
    	name:=Name{FirstName:"Zhang",Surname:"san"}
    	employee:=Employee{Position:"China",Name: name}
    	person,err:=json.Marshal(employee)
    	if err!=nil{
    		panic(errors.New("Wrong Converting behavior"))
    	}
    	log.Println(string(person))
    }
    

    JSON格式数据转换为Golang结构体的过程,利用Json.Ummarshal函数进行转换,具体转换例子如下:

    package message
    
    import (
    	"encoding/json"
    	"log"
    	"testing"
    )
    
    type Information struct{
    	Name string `json:"name"`
    	Addr string `json:"addr"`
    }
    
    func TestStructure(t *testing.T){
    	alice:="{"name":"Alice","addr":"Green Street"}"
    	inf:=new(Information)
    	err:=json.Unmarshal([]byte(alice),inf)
    	if err!=nil{
    		panic(err)
    	}
    	log.Println(inf)
    }
    

    类似于嵌套结构体转换到JSON格式的过程,从JSON格式数据转换到嵌套结构体数据的过程,就是首先构建嵌套JSON格式数据,其次通过Json.Ummarshal函数转换为嵌套结构体数据。

    package message
    
    import (
    	"encoding/json"
    	"log"
    	"testing"
    )
    
    type Employee struct {
    	Position string `json:"position"`
    	Name     Name   `json:"name"`
    }
    
    type Name struct {
    	FirstName string `json:"firstname"`
    	Surname   string `json:"surname"`
    }
    
    func TestStructure(t *testing.T){
    	str:="{"position":"China","name":{"firstname":"Zhang","surname":"san"}}"
    	person1:=new(Employee)
    	json.Unmarshal([]byte(str),person1)
    	log.Println(person1,person1.Name)
    }
    

    以上就是JSon格式数据和Golang结构体之间的数据转换过程。

    到此这篇关于Golang实现结构体和Json格式数据之间的互相转换的文章就介绍到这了,更多相关Golang结构体和Json互转内容请搜索风君子博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持风君子博客!

    您可能感兴趣的文章:

    • Golang实现自定义时间结构体并支持Json&Gorm
    • Golang实现Json转结构体的示例详解
    • 详解Go语言中结构体与JSON间的转换
    • go语言通过结构体生成json示例解析
    • Go 结构体、数组、字典和 json 字符串的相互转换方法
    • go语言使用第三方包 json化结构体操作示例
    • golang结构体与json格式串实例代码

    站内搜索