github.com/gogf/gf@v1.16.9/encoding/gjson/gjson_z_example_new_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gjson_test 8 9 import ( 10 "fmt" 11 "github.com/gogf/gf/encoding/gjson" 12 ) 13 14 func Example_newFromJson() { 15 jsonContent := `{"name":"john", "score":"100"}` 16 j := gjson.New(jsonContent) 17 fmt.Println(j.Get("name")) 18 fmt.Println(j.Get("score")) 19 // Output: 20 // john 21 // 100 22 } 23 24 func Example_newFromXml() { 25 jsonContent := `<?xml version="1.0" encoding="UTF-8"?><doc><name>john</name><score>100</score></doc>` 26 j := gjson.New(jsonContent) 27 // Note that there's root node in the XML content. 28 fmt.Println(j.Get("doc.name")) 29 fmt.Println(j.Get("doc.score")) 30 // Output: 31 // john 32 // 100 33 } 34 35 func Example_newFromStruct() { 36 type Me struct { 37 Name string `json:"name"` 38 Score int `json:"score"` 39 } 40 me := Me{ 41 Name: "john", 42 Score: 100, 43 } 44 j := gjson.New(me) 45 fmt.Println(j.Get("name")) 46 fmt.Println(j.Get("score")) 47 // Output: 48 // john 49 // 100 50 } 51 52 func Example_newFromStructWithTag() { 53 type Me struct { 54 Name string `tag:"name"` 55 Score int `tag:"score"` 56 Title string 57 } 58 me := Me{ 59 Name: "john", 60 Score: 100, 61 Title: "engineer", 62 } 63 // The parameter <tags> specifies custom priority tags for struct conversion to map, 64 // multiple tags joined with char ','. 65 j := gjson.NewWithTag(me, "tag") 66 fmt.Println(j.Get("name")) 67 fmt.Println(j.Get("score")) 68 fmt.Println(j.Get("Title")) 69 // Output: 70 // john 71 // 100 72 // engineer 73 }