import (
"encoding/json"
"fmt"
)
//父对象
type Human struct {
Name string `json:"english_name"` //把struct编码成json字符串时,Name字段的key是english_name,而不是Name
Age int `json:"-"` //把struct编码成json字符串时,会忽略Age这个字段
Weight float64 //`json:",string"` //编码成字符串,这个string选项只适用字符串,浮点型和整型数据
}
//子对象
type Student struct {
Human
Speciality string
Hobby []string
score float64 `json:"total,omitempty"` //如果这个字段是空值,则不编码到JSON里面,否则用total为名字编码
Secret []byte `json:",omitempty"` //如果这个字段是空值,则不编码到JSON里面,否则用Secret为名字编码
//是一个经过base64编码的字符串
}
func main() {
json1Str := `[
{"Name": "James","Age": 22,"Weight":65.80},
{"english_name": "Jack","Age": 27,"Weight":78.50},
{"Name": "Kite","Age": 19,"Weight":61.40}]`
//定义一个切片对象
var humans []Human
//json字符串转换成对象格式
err1 := json.Unmarshal([]byte(json1Str),&humans)
if err1 != nil {
fmt.Println(err1)
}
fmt.Println("json字符串转换成对象格式")
fmt.Println(humans)
fmt.Println("对象集合转换成json字符串")
//切片初始化3个对象
slice1 := []Human{
Human{Name: "James",Age: 22,Weight: 65.80},
Human{Name: "Jack",Age: 27,Weight: 78.50},
Human{Name: "Kite",Age: 19,Weight: 61.40}}
//对象集合转换成json字符串
data1,err2 := json.Marshal(slice1)
if err2 != nil {
fmt.Println(err2)
}
fmt.Println(string(data1))
fmt.Println("=======================================================================================")
stus := make([]Student,0)
jason := Student{
Human: Human{
Name: "jason",
Age: 28,
Weight: 60.30},
Speciality: "computer science",
Hobby: []string{"tennis","swimming","reading"},
score: 725.5,
Secret: []byte{'I',20,'L','o','v','e','Y','u','!'}}
stus = append(stus,jason)
fmt.Println(stus,"\n\r")
//对象集合转换成json字符串
data2,err3 := json.Marshal(stus[0])
if err3 != nil {
fmt.Println(err3)
}
fmt.Println(string(data2))
fmt.Println("=======================================================================================")
json2Str := `[
{"english_name":"jason","Weight":60.3,"Speciality":"computer science","Hobby":["tennis","reading"],"total":725.5,"Secret":"SRRMb3ZlFFlvdSE="}]`
//定义一个切片对象
var stuss []Student
//json字符串转换成对象格式
err4 := json.Unmarshal([]byte(json2Str),&stuss)
if err4 != nil {
fmt.Println(err4)
}
fmt.Println("json字符串转换成对象格式")
fmt.Println(stuss)
}
范例 如何获得一个这样的json格式的数据{"code":200,"msg":"ok","data":{"content":"test"}}
package main
import (
"encoding/json"
"fmt"
)
type Header struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data `json:"data"`
}
type Data struct {
Content string `json:"content"`
}
func main() {
in := Header{Code: 200,Msg: "ok",Data: Data{Content: "test"}}
out,_ := json.Marshal(&in)
json := string(out)
fmt.Println(json)
}
{"asks":[[1671.7000,24.4247],[1671.8000,1.2068],[1672.0000,2.0000]]} 请问这样的Json,对应的数据结构要怎么定义?
使用Map实现获得同样的结果实现:
package main
import (
"encoding/json"
"fmt"
)
func main() {
data := make(map[string][][]float64)
// data["asks"] = [][]float64{[]float64{1671.7000,24.4247},[]float64{1671.8000,1.2068},[]float64{1672.0000,2.0000}}
data["asks"] = [][]float64{{1671.7000,{1671.8000,{1672.0000,2.0000}} // 简写格式
out,_ := json.Marshal(data)
json := string(out)
fmt.Println(json)
}
调试控制台打印输出如下信息:
2017/07/10 11:27:44 server.go:73: Using API v1
2017/07/10 11:27:44 debugger.go:97: launching process with args: [/root/code/go/src/contoso.org/Gooo/debug www.baidu.com www.ifeng.com www.sina.com.cn www.qq.com www.taobao.com www.suning.com www.163.com www.iqiyi.com sports.cntv.cn www.jd.com]
API server listening at: 127.0.0.1:2345
2017/07/10 11:27:45 debugger.go:505: continuing
{"asks":[[1671.7,[1671.8,[1672,2]]}