Go语言第十六课 JSON

type类型转JSON string(编码)

Go核心库已经对JSON有比较好的支持,编码部分是由核心库encoding/json支持的。

package main

import (
	"encoding/json"
	"fmt"
)

type persion struct {
	Name    string
	Age     int
	Gender  string
	School  string
	Married bool
}

func main() {
	var yong persion
	yong.Age = 26
	yong.Name = "Yong.Yu"
	yong.Gender = "M"
	yong.School = "USC"
	yong.Married = true
	j_yong, err := json.Marshal(yong)
	if err == nil {
		fmt.Printf("%s\n", j_yong)
	}
}

结果:

{"Name":"Yong.Yu","Age":26,"Gender":"M","School":"USC","Married":true}

* 注意:由于编码实际上用到的是反射,所以类型里需要编码的属性必须是可导出的,也就是必须以大写开头。如上面的例子,如果将persion类型的Name属性改成小写name,再编码结果为:{"Age":26,"Gender":"M","School":"USC","Married":true}。这是因为name不可导出,自然就无法编码。

默认编码出来的key是属性的属性名,但是这一点可以更改。

package main

import (
	"encoding/json"
	"fmt"
)

type persion struct {
	Name    string
	Age     int
	Gender  string
	School  string `json:"school-name"`
	Married bool
}

func main() {
	var yong persion
	yong.Age = 26
	yong.Name = "Yong.Yu"
	yong.Gender = "M"
	yong.School = "USC"
	yong.Married = true
	j_yong, err := json.Marshal(yong)
	if err == nil {
		fmt.Printf("%s\n", j_yong)
	}
}

结果:

{"Name":"Yong.Yu","Age":26,"Gender":"M","school-name":"USC","Married":true}

显然,如果没有对某个属性赋值,这个属性将被赋默认零值。这个零值也会出现在编码之后的JSON中。但是这一点可以更改,借助omitempty,JSON内将不包含那些零值的属性。

package main

import (
	"encoding/json"
	"fmt"
)

type persion struct {
	Name    string
	Age     int    `json:"age,omitempty"`
	Gender  string
	School  string `json:"school-name"`
	Married bool
}

func main() {
	var yong persion
	yong.Age = 26
	yong.Name = "Yong.Yu"
	yong.Gender = "M"
	yong.School = "USC"
	yong.Married = true
	j_yong, err := json.Marshal(yong)
	if err == nil {
		fmt.Printf("%s\n", j_yong)
	}
	//-------------------------------------
	var yong1 persion
	yong1.Name = "Yong1.Yu"
	yong1.Gender = "M"
	yong1.School = "USC"
	yong1.Married = true
	j_yong, err = json.Marshal(yong1)
	if err == nil {
		fmt.Printf("%s\n", j_yong)
	}
}

结果:

{"Name":"Yong.Yu","age":26,"Gender":"M","school-name":"USC","Married":true}

{"Name":"Yong1.Yu","Gender":"M","school-name":"USC","Married":true}

解码操作刚好相反

package main

import (
	"encoding/json"
	"fmt"
)

type persion struct {
	Name    string
	Age     int    `json:"age,omitempty"`
	Gender  string
	School  string `json:"school-name"`
	Married bool
}

func main() {
	var yong persion
	yong.Age = 26
	yong.Name = "Yong.Yu"
	yong.Gender = "M"
	yong.School = "USC"
	yong.Married = true
	j_yong, err := json.Marshal(yong)
	if err == nil {
		fmt.Printf("%s\n", j_yong)
	}
	//-------------------------------------
	var p_yong *persion = new(persion)
	json.Unmarshal([]byte(j_yong), p_yong)
	fmt.Println(p_yong.Name)
	fmt.Println(p_yong.Age)
}

结果

{"Name":"Yong.Yu","age":26,"Gender":"M","school-name":"USC","Married":true}
Yong.Yu
26

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值