github.com/wangyougui/gf/v2@v2.6.5/encoding/gjson/gjson_z_example_load_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/wangyougui/gf. 6 7 package gjson_test 8 9 import ( 10 "fmt" 11 12 "github.com/wangyougui/gf/v2/encoding/gjson" 13 "github.com/wangyougui/gf/v2/test/gtest" 14 ) 15 16 func ExampleLoad() { 17 jsonFilePath := gtest.DataPath("json", "data1.json") 18 j, _ := gjson.Load(jsonFilePath) 19 fmt.Println(j.Get("name")) 20 fmt.Println(j.Get("score")) 21 22 notExistFilePath := gtest.DataPath("json", "data2.json") 23 j2, _ := gjson.Load(notExistFilePath) 24 fmt.Println(j2.Get("name")) 25 26 // Output: 27 // john 28 // 100 29 } 30 31 func ExampleLoadJson() { 32 jsonContent := `{"name":"john", "score":"100"}` 33 j, _ := gjson.LoadJson(jsonContent) 34 fmt.Println(j.Get("name")) 35 fmt.Println(j.Get("score")) 36 37 // Output: 38 // john 39 // 100 40 } 41 42 func ExampleLoadXml() { 43 xmlContent := `<?xml version="1.0" encoding="UTF-8"?> 44 <base> 45 <name>john</name> 46 <score>100</score> 47 </base>` 48 j, _ := gjson.LoadXml(xmlContent) 49 fmt.Println(j.Get("base.name")) 50 fmt.Println(j.Get("base.score")) 51 52 // Output: 53 // john 54 // 100 55 } 56 57 func ExampleLoadIni() { 58 iniContent := ` 59 [base] 60 name = john 61 score = 100 62 ` 63 j, _ := gjson.LoadIni(iniContent) 64 fmt.Println(j.Get("base.name")) 65 fmt.Println(j.Get("base.score")) 66 67 // Output: 68 // john 69 // 100 70 } 71 72 func ExampleLoadYaml() { 73 yamlContent := 74 `base: 75 name: john 76 score: 100` 77 78 j, _ := gjson.LoadYaml(yamlContent) 79 fmt.Println(j.Get("base.name")) 80 fmt.Println(j.Get("base.score")) 81 82 // Output: 83 // john 84 // 100 85 } 86 87 func ExampleLoadToml() { 88 tomlContent := 89 `[base] 90 name = "john" 91 score = 100` 92 93 j, _ := gjson.LoadToml(tomlContent) 94 fmt.Println(j.Get("base.name")) 95 fmt.Println(j.Get("base.score")) 96 97 // Output: 98 // john 99 // 100 100 } 101 102 func ExampleLoadContent() { 103 jsonContent := `{"name":"john", "score":"100"}` 104 105 j, _ := gjson.LoadContent(jsonContent) 106 107 fmt.Println(j.Get("name")) 108 fmt.Println(j.Get("score")) 109 110 // Output: 111 // john 112 // 100 113 } 114 115 func ExampleLoadContent_UTF8BOM() { 116 jsonContent := `{"name":"john", "score":"100"}` 117 118 content := make([]byte, 3, len(jsonContent)+3) 119 content[0] = 0xEF 120 content[1] = 0xBB 121 content[2] = 0xBF 122 content = append(content, jsonContent...) 123 124 j, _ := gjson.LoadContent(content) 125 126 fmt.Println(j.Get("name")) 127 fmt.Println(j.Get("score")) 128 129 // Output: 130 // john 131 // 100 132 } 133 134 func ExampleLoadContent_Xml() { 135 xmlContent := `<?xml version="1.0" encoding="UTF-8"?> 136 <base> 137 <name>john</name> 138 <score>100</score> 139 </base>` 140 141 x, _ := gjson.LoadContent(xmlContent) 142 143 fmt.Println(x.Get("base.name")) 144 fmt.Println(x.Get("base.score")) 145 146 // Output: 147 // john 148 // 100 149 } 150 151 func ExampleLoadContentType() { 152 jsonContent := `{"name":"john", "score":"100"}` 153 xmlContent := `<?xml version="1.0" encoding="UTF-8"?> 154 <base> 155 <name>john</name> 156 <score>100</score> 157 </base>` 158 159 j, _ := gjson.LoadContentType("json", jsonContent) 160 x, _ := gjson.LoadContentType("xml", xmlContent) 161 j1, _ := gjson.LoadContentType("json", "") 162 163 fmt.Println(j.Get("name")) 164 fmt.Println(j.Get("score")) 165 fmt.Println(x.Get("base.name")) 166 fmt.Println(x.Get("base.score")) 167 fmt.Println(j1.Get("")) 168 169 // Output: 170 // john 171 // 100 172 // john 173 // 100 174 } 175 176 func ExampleIsValidDataType() { 177 fmt.Println(gjson.IsValidDataType("json")) 178 fmt.Println(gjson.IsValidDataType("yml")) 179 fmt.Println(gjson.IsValidDataType("js")) 180 fmt.Println(gjson.IsValidDataType("mp4")) 181 fmt.Println(gjson.IsValidDataType("xsl")) 182 fmt.Println(gjson.IsValidDataType("txt")) 183 fmt.Println(gjson.IsValidDataType("")) 184 fmt.Println(gjson.IsValidDataType(".json")) 185 fmt.Println(gjson.IsValidDataType(".properties")) 186 187 // Output: 188 // true 189 // true 190 // true 191 // false 192 // false 193 // false 194 // false 195 // true 196 // true 197 } 198 199 func ExampleLoad_Xml() { 200 jsonFilePath := gtest.DataPath("xml", "data1.xml") 201 j, _ := gjson.Load(jsonFilePath) 202 fmt.Println(j.Get("doc.name")) 203 fmt.Println(j.Get("doc.score")) 204 } 205 206 func ExampleLoad_Properties() { 207 jsonFilePath := gtest.DataPath("properties", "data1.properties") 208 j, _ := gjson.Load(jsonFilePath) 209 fmt.Println(j.Get("pr.name")) 210 fmt.Println(j.Get("pr.score")) 211 fmt.Println(j.Get("pr.sex")) 212 213 //Output: 214 // john 215 // 100 216 // 0 217 }