go json使用

package main

import (
	"encoding/json"
	"fmt"
)

type DateValue map[string]string

type A struct {
	Str     string
	IntVal  int
	Str_vec []string
	Time    string `json:"time"`
	Vec_map map[int]DateValue
}

func main() {
	var vec_map map[int]DateValue
	vec_map = make(map[int]DateValue)

	var dv DateValue
	dv = make(map[string]string)
	dv["key1"] = "value1"
	dv["key2"] = "value1"

	vec_map[0] = dv
	a := &A{
		"aaa",
		111,
		[]string{"str1", "str2"},
		"ccc",
		vec_map,
	}

	data, err := json.Marshal(a)
	if err == nil {
		println("json data:", string(data))
	}

	var a1 A
	err = json.Unmarshal([]byte(data), &a1)
	if err == nil {
		fmt.Println("unmarsha1:", a1)
	}
}

json data: {"Str":"aaa","IntVal":111,"Str_vec":["str1","str2"],"time":"ccc","Vec_map":{"0":{"key1":"value1","key2":"value1"}}}
unmarsha1: {aaa 111 [str1 str2] ccc map[0:map[key1:value1 key2:value1]]}

猜你喜欢

转载自xiangjie88.iteye.com/blog/2388288