github.com/zhangdapeng520/zdpgo_json@v0.1.5/README.md (about) 1 # zdpgo_json 2 3 在 Golang 中便捷从处理 Json 文件,比如动态配置,增删改查 4 5 项目地址:https://github.com/zhangdapeng520/zdpgo_json 6 7 ## 功能清单 8 9 - 与 Python 保持一致的 API 接口,dump 对应 Dump,load 对应 Load,dumps 对应 Dumps,loads 对应 Loads 10 - 支持 json 字符串的查询 11 12 ## 版本历史 13 14 - v0.1.0 2022/02/16 基本功能 15 - v0.1.1 2022/03/30 读取配置 16 - v0.1.2 2022/04/02 项目结构优化 17 - v0.1.3 2022/06/16 优化:读取 json 字符串优化 18 - v0.1.4 2022/06/22 新增:整合 jsoniter,序列化性能极大提升 19 - v0.1.5 2022/07/14 优化:代码优化 20 21 ## 使用示例 22 23 ### 读写 json 文件 24 25 ```go 26 package main 27 28 import ( 29 "fmt" 30 31 "github.com/zhangdapeng520/zdpgo_json" 32 ) 33 34 type account struct { 35 Email string `json:"email"` 36 password string `json:"password"` // 不会处理私有变量 37 Money float64 `json:"money"` 38 } 39 40 type user struct { 41 Name string 42 Age int 43 Roles []string 44 Skill map[string]float64 45 Account account 46 } 47 48 func main() { 49 a := account{ 50 Email: "张大鹏", 51 password: "123456", 52 Money: 100.5, 53 } 54 u := user{ 55 Name: "张大鹏", 56 Age: 27, 57 Roles: []string{"Owner", "Master"}, // 处理切片 58 Account: a, 59 } 60 61 // 序列化 62 jsonData, err := zdpgo_json.Dumps(u) 63 if err != nil { 64 fmt.Println(err) 65 } 66 fmt.Println(jsonData) 67 68 // 反序列化 69 err = zdpgo_json.Loads(jsonData, &u) 70 if err != nil { 71 fmt.Println(err) 72 } 73 fmt.Println(u) 74 } 75 ``` 76 77 ### 序列化和反序列化 78 79 ```go 80 package main 81 82 import ( 83 "fmt" 84 85 "github.com/zhangdapeng520/zdpgo_json" 86 ) 87 88 type account struct { 89 Email string `json:"email"` 90 password string `json:"password"` // 不会处理私有变量 91 Money float64 `json:"money"` 92 } 93 94 type user struct { 95 Name string 96 Age int 97 Roles []string 98 Skill map[string]float64 99 Account account 100 } 101 102 func main() { 103 a := account{ 104 Email: "张大鹏", 105 password: "123456", 106 Money: 100.5, 107 } 108 u := user{ 109 Name: "张大鹏", 110 Age: 27, 111 Roles: []string{"Owner", "Master"}, // 处理切片 112 Account: a, 113 } 114 115 // 写入文件 116 err := zdpgo_json.Dump("user.json", u) 117 if err != nil { 118 fmt.Println(err) 119 } 120 121 // 读取文件 122 err = zdpgo_json.Load("user.json", &u) 123 if err != nil { 124 fmt.Println(err) 125 } 126 fmt.Println(u) 127 } 128 ``` 129 130 ### 从 json 字符串中查询数据 131 132 ```go 133 package main 134 135 import ( 136 "fmt" 137 138 "github.com/zhangdapeng520/zdpgo_json" 139 ) 140 141 type account struct { 142 Email string `json:"email"` 143 password string `json:"password"` // 不会处理私有变量 144 Money float64 `json:"money"` 145 } 146 147 type user struct { 148 Name string 149 Age int 150 Roles []string 151 Skill map[string]float64 152 Account account 153 } 154 155 func main() { 156 a := account{ 157 Email: "张大鹏", 158 password: "123456", 159 Money: 100.5, 160 } 161 u := user{ 162 Name: "张大鹏", 163 Age: 27, 164 Roles: []string{"Owner", "Master"}, // 处理切片 165 Account: a, 166 } 167 168 j := zdpgo_json.New() 169 170 // 序列化 171 jsonData, err := j.Dumps(u) 172 if err != nil { 173 fmt.Println(err) 174 } 175 fmt.Println(jsonData) 176 fmt.Println("the json string :", jsonData) 177 178 // Get查询 179 money := j.Query.Get(jsonData, "Account.money") 180 fmt.Println("Get查询:", money, money.Float()) 181 } 182 ```