@Test publicvoidloadMap()throws IOException { Yaml yaml = new Yaml(); Map<String, Object> map = null; InputStream is = new FileInputStream(new File("/Users/wangzulong/mycode/yamtest/src/main/resources/testload.yaml")); if (is != null) { map = (Map<String, Object>) yaml.load(is); is.close(); } System.out.println(map); }
@Test publicvoidloadObjects()throws IOException { Yaml yaml = new Yaml(); List<Object> list = null; InputStream is = new FileInputStream(new File("src/test/resources/example.yaml")); if (is != null) { list = new ArrayList<Object>(); for (Object obj : yaml.loadAll(is)) { list.add(obj); } is.close(); }
type Person struct { Name string`json:"username"` Age int Gender bool`json:",omitempty"` Profile string OmitContent string`json:"-"` Count int`json:",string"` }
funcmain() { var p *Person = &Person{ Name: "brainwu", Age: 21, Gender: true, Profile: "I am Wujunbin", OmitContent: "OmitConent", } if bs, err := json.Marshal(&p); err != nil { panic(err) } else { //result --> {"username":"brainwu","Age":21,"Gender":true,"Profile":"I am Wujunbin","Count":"0"} fmt.Println(string(bs)) }
var p2 *Person = &Person{ Name: "brainwu", Age: 21, Profile: "I am Wujunbin", OmitContent: "OmitConent", } if bs, err := json.Marshal(&p2); err != nil { panic(err) } else { //result --> {"username":"brainwu","Age":21,"Profile":"I am Wujunbin","Count":"0"} fmt.Println(string(bs)) }
Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v. If v is nil or not a pointer, Unmarshal returns an InvalidUnmarshalError. Unmarshal用于反序列化json的函数,根据data将数据反序列化到传入的对象中
funcmain() { type Person struct { Name string Age int Gender bool } //unmarshal to struct var p Person var str = `{"Name":"junbin", "Age":21, "Gender":true}` json.Unmarshal([]byte(str), &p) //result --> junbin : 21 : true fmt.Println(p.Name, ":", p.Age, ":", p.Gender)
// unmarshal to slice-struct var ps []Person var aJson = `[{"Name":"junbin", "Age":21, "Gender":true}, {"Name":"junbin", "Age":21, "Gender":true}]` json.Unmarshal([]byte(aJson), &ps) //result --> [{junbin 21 true} {junbin 21 true}] len is 2 fmt.Println(ps, "len is", len(ps))
// unmarshal to map[string]interface{} var obj interface{} // var obj map[string]interface{} json.Unmarshal([]byte(str), &obj) m := obj.(map[string]interface{}) //result --> junbin : 21 : true fmt.Println(m["Name"], ":", m["Age"], ":", m["Gender"])
//unmarshal to slice var strs string = `["Go", "Java", "C", "Php"]` var aStr []string json.Unmarshal([]byte(strs), &aStr) //result --> [Go Java C Php] len is 4 fmt.Println(aStr, " len is", len(aStr)) }
var i interface{} i = "a string" i = 2011 i = 2.777
类型断言可以访问底层的具体类型:
1 2
r := i.(float64) fmt.Println("the circle's area", math.Pi*r*r)
或者,如果底层的类型是未知的,通过类型switch来确定类型:
1 2 3 4 5 6 7 8 9 10 11
switch v := i.(type) { caseint: fmt.Println("twice i is", v*2) casefloat64: fmt.Println("the reciprocal of i is", 1/v) casestring: h := len(v) / 2 fmt.Println("i swapped by halves is", v[h:]+v[:h]) default: // i isn't one of the types above }
for k, v := range m { switch vv := v.(type) { casestring: fmt.Println(k, "is string", vv) caseint: fmt.Println(k, "is int", vv) case []interface{}: fmt.Println(k, "is an array:") for i, u := range vv { fmt.Println(i, u) } default: fmt.Println(k, "is of a type I don't know how to handle") } }
funcmain() { type Color struct { Space string Point json.RawMessage // delay parsing until we know the color space } type RGB struct { R uint8 G uint8 B uint8 } type YCbCr struct { Y uint8 Cb int8 Cr int8 }
funcmain() { dec := json.NewDecoder(os.Stdin) enc := json.NewEncoder(os.Stdout) for { var v map[string]interface{} if err := dec.Decode(&v); err != nil { log.Println(err) return } for k := range v { if k != "Name" { delete(v, k) } } if err := enc.Encode(&v); err != nil { log.Println(err) } } }