编解码JSON数据

package main

import (
	_ "fmt"
	"encoding/json"
	"log"
)

type Book struct {
	Title string
	Authors []string
	Publisher string
	IsPublished bool
	Price float32
}

func main() {
	gobook := Book {
		"Go语言编程",
		[]string{"XuShiwei", "HughLv", "Pandaman", "GuaguaSong", "HanTuo",  
			"BertYuan", "XuDaoli"},
		"ituring.com.cn",
		true,
		9.99,
	}
	
	b, _ := json.Marshal(gobook)
	log.Printf("b = %#v\n", b)
	
	// comparedByteArray就是b进行JSON格式化后的[]byte
	comparedByteArray := []byte(`{"Title":"Go语言编程","Authors":["XuShiwei","HughLv","Pandaman","GuaguaSong","HanTuo","BertYuan","XuDaoli"],"Publisher":"ituring.com.cn","IsPublished":true,"Price":9.99}`)
	
	if comparedByteArray != nil {
		log.Printf("comparedByteArray = %#v\n", comparedByteArray)
	}
	
	for i, _ := range b {
		if b[i] != comparedByteArray[i] {
			log.Println("b[", i, "] = ", b[i], ", ",
				"comparedByteArray[", i, "] = ", comparedByteArray[i])
		}
	}
	
	// 如果JSON中的字段在Go目标类型中不存在,json.UnMarshal()函数在解码过程中会丢弃该字段。
	// 由于Sales字段并没有在Book类型中定义,所以会被忽略,只有Title这个字段的值会被填充到book.Title中。
	b = []byte(`{"Title":"Go编程语言","Sales":1000000}`)
	var book Book
	err := json.Unmarshal(b, &book)
	if err == nil {
		log.Println(book)
	}
	
}


输出结果

2015/12/06 12:52:36 b = []byte{0x7b, 0x22, 0x54, 0x69, 0x74, 0x6c, 0x65, 0x22, 0x3a, 0x22, 0x47, 0x6f, 0xe8, 0xaf, 0xad, 0xe8, 0xa8, 0x80, 0xe7, 0xbc, 0x96, 0xe7, 0xa8, 0x8b, 0x22, 0x2c, 0x22, 0x41, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x73, 0x22, 0x3a, 0x5b, 0x22, 0x58, 0x75, 0x53, 0x68, 0x69, 0x77, 0x65, 0x69, 0x22, 0x2c, 0x22, 0x48, 0x75, 0x67, 0x68, 0x4c, 0x76, 0x22, 0x2c, 0x22, 0x50, 0x61, 0x6e, 0x64, 0x61, 0x6d, 0x61, 0x6e, 0x22, 0x2c, 0x22, 0x47, 0x75, 0x61, 0x67, 0x75, 0x61, 0x53, 0x6f, 0x6e, 0x67, 0x22, 0x2c, 0x22, 0x48, 0x61, 0x6e, 0x54, 0x75, 0x6f, 0x22, 0x2c, 0x22, 0x42, 0x65, 0x72, 0x74, 0x59, 0x75, 0x61, 0x6e, 0x22, 0x2c, 0x22, 0x58, 0x75, 0x44, 0x61, 0x6f, 0x6c, 0x69, 0x22, 0x5d, 0x2c, 0x22, 0x50, 0x75, 0x62, 0x6c, 0x69, 0x73, 0x68, 0x65, 0x72, 0x22, 0x3a, 0x22, 0x69, 0x74, 0x75, 0x72, 0x69, 0x6e, 0x67, 0x2e, 0x63, 0x6f, 0x6d, 0x2e, 0x63, 0x6e, 0x22, 0x2c, 0x22, 0x49, 0x73, 0x50, 0x75, 0x62, 0x6c, 0x69, 0x73, 0x68, 0x65, 0x64, 0x22, 0x3a, 0x74, 0x72, 0x75, 0x65, 0x2c, 0x22, 0x50, 0x72, 0x69, 0x63, 0x65, 0x22, 0x3a, 0x39, 0x2e, 0x39, 0x39, 0x7d}
2015/12/06 12:52:36 comparedByteArray = []byte{0x7b, 0x22, 0x54, 0x69, 0x74, 0x6c, 0x65, 0x22, 0x3a, 0x22, 0x47, 0x6f, 0xe8, 0xaf, 0xad, 0xe8, 0xa8, 0x80, 0xe7, 0xbc, 0x96, 0xe7, 0xa8, 0x8b, 0x22, 0x2c, 0x22, 0x41, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x73, 0x22, 0x3a, 0x5b, 0x22, 0x58, 0x75, 0x53, 0x68, 0x69, 0x77, 0x65, 0x69, 0x22, 0x2c, 0x22, 0x48, 0x75, 0x67, 0x68, 0x4c, 0x76, 0x22, 0x2c, 0x22, 0x50, 0x61, 0x6e, 0x64, 0x61, 0x6d, 0x61, 0x6e, 0x22, 0x2c, 0x22, 0x47, 0x75, 0x61, 0x67, 0x75, 0x61, 0x53, 0x6f, 0x6e, 0x67, 0x22, 0x2c, 0x22, 0x48, 0x61, 0x6e, 0x54, 0x75, 0x6f, 0x22, 0x2c, 0x22, 0x42, 0x65, 0x72, 0x74, 0x59, 0x75, 0x61, 0x6e, 0x22, 0x2c, 0x22, 0x58, 0x75, 0x44, 0x61, 0x6f, 0x6c, 0x69, 0x22, 0x5d, 0x2c, 0x22, 0x50, 0x75, 0x62, 0x6c, 0x69, 0x73, 0x68, 0x65, 0x72, 0x22, 0x3a, 0x22, 0x69, 0x74, 0x75, 0x72, 0x69, 0x6e, 0x67, 0x2e, 0x63, 0x6f, 0x6d, 0x2e, 0x63, 0x6e, 0x22, 0x2c, 0x22, 0x49, 0x73, 0x50, 0x75, 0x62, 0x6c, 0x69, 0x73, 0x68, 0x65, 0x64, 0x22, 0x3a, 0x74, 0x72, 0x75, 0x65, 0x2c, 0x22, 0x50, 0x72, 0x69, 0x63, 0x65, 0x22, 0x3a, 0x39, 0x2e, 0x39, 0x39, 0x7d}
2015/12/06 12:52:36 {Go编程语言 [] false 0}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值