github.com/gogf/gf/v2@v2.7.4/encoding/gjson/gjson_z_example_pattern_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 12 "github.com/gogf/gf/v2/encoding/gjson" 13 ) 14 15 func ExampleDecodeToJson_PatternGet() { 16 data := 17 `{ 18 "users" : { 19 "count" : 2, 20 "list" : [ 21 {"name" : "Ming", "score" : 60}, 22 {"name" : "John", "score" : 99.5} 23 ] 24 } 25 }` 26 if j, err := gjson.DecodeToJson(data); err != nil { 27 panic(err) 28 } else { 29 fmt.Println("John Score:", j.Get("users.list.1.score").Float32()) 30 } 31 // Output: 32 // John Score: 99.5 33 } 34 35 func ExampleDecodeToJson_PatternViolenceCheck() { 36 data := 37 `{ 38 "users" : { 39 "count" : 100 40 }, 41 "users.count" : 101 42 }` 43 if j, err := gjson.DecodeToJson(data); err != nil { 44 panic(err) 45 } else { 46 j.SetViolenceCheck(true) 47 fmt.Println("Users Count:", j.Get("users.count").Int()) 48 } 49 // Output: 50 // Users Count: 101 51 } 52 53 func ExampleJson_Get_MapSliceChange() { 54 jsonContent := `{"map":{"key":"value"}, "slice":[59,90]}` 55 j, _ := gjson.LoadJson(jsonContent) 56 m := j.Get("map").Map() 57 fmt.Println(m) 58 59 // Change the key-value pair. 60 m["key"] = "john" 61 62 // It changes the underlying key-value pair. 63 fmt.Println(j.Get("map").Map()) 64 65 s := j.Get("slice").Array() 66 fmt.Println(s) 67 68 // Change the value of specified index. 69 s[0] = 100 70 71 // It changes the underlying slice. 72 fmt.Println(j.Get("slice").Array()) 73 74 // output: 75 // map[key:value] 76 // map[key:john] 77 // [59 90] 78 // [100 90] 79 }