github.com/gogf/gf/v2@v2.7.4/encoding/gjson/gjson_z_unit_feature_struct_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 "testing" 11 12 "github.com/gogf/gf/v2/encoding/gjson" 13 "github.com/gogf/gf/v2/test/gtest" 14 ) 15 16 func Test_GetScan(t *testing.T) { 17 type User struct { 18 Name string 19 Score float64 20 } 21 j := gjson.New(`[{"name":"john", "score":"100"},{"name":"smith", "score":"60"}]`) 22 gtest.C(t, func(t *gtest.T) { 23 var user *User 24 err := j.Get("1").Scan(&user) 25 t.AssertNil(err) 26 t.Assert(user, &User{ 27 Name: "smith", 28 Score: 60, 29 }) 30 }) 31 gtest.C(t, func(t *gtest.T) { 32 var users []User 33 err := j.Get(".").Scan(&users) 34 t.AssertNil(err) 35 t.Assert(users, []User{ 36 { 37 Name: "john", 38 Score: 100, 39 }, 40 { 41 Name: "smith", 42 Score: 60, 43 }, 44 }) 45 }) 46 } 47 48 func Test_GetScanDeep(t *testing.T) { 49 type User struct { 50 Name string 51 Score float64 52 } 53 j := gjson.New(`[{"name":"john", "score":"100"},{"name":"smith", "score":"60"}]`) 54 gtest.C(t, func(t *gtest.T) { 55 var user *User 56 err := j.Get("1").Scan(&user) 57 t.AssertNil(err) 58 t.Assert(user, &User{ 59 Name: "smith", 60 Score: 60, 61 }) 62 }) 63 gtest.C(t, func(t *gtest.T) { 64 var users []User 65 err := j.Get(".").Scan(&users) 66 t.AssertNil(err) 67 t.Assert(users, []User{ 68 { 69 Name: "john", 70 Score: 100, 71 }, 72 { 73 Name: "smith", 74 Score: 60, 75 }, 76 }) 77 }) 78 } 79 80 func Test_Scan1(t *testing.T) { 81 type User struct { 82 Name string 83 Score float64 84 } 85 j := gjson.New(`[{"name":"john", "score":"100"},{"name":"smith", "score":"60"}]`) 86 gtest.C(t, func(t *gtest.T) { 87 var users []User 88 err := j.Var().Scan(&users) 89 t.AssertNil(err) 90 t.Assert(users, []User{ 91 { 92 Name: "john", 93 Score: 100, 94 }, 95 { 96 Name: "smith", 97 Score: 60, 98 }, 99 }) 100 }) 101 } 102 103 func Test_Scan2(t *testing.T) { 104 type User struct { 105 Name string 106 Score float64 107 } 108 j := gjson.New(`[{"name":"john", "score":"100"},{"name":"smith", "score":"60"}]`) 109 gtest.C(t, func(t *gtest.T) { 110 var users []User 111 err := j.Var().Scan(&users) 112 t.AssertNil(err) 113 t.Assert(users, []User{ 114 { 115 Name: "john", 116 Score: 100, 117 }, 118 { 119 Name: "smith", 120 Score: 60, 121 }, 122 }) 123 }) 124 } 125 126 func Test_Struct1(t *testing.T) { 127 gtest.C(t, func(t *gtest.T) { 128 type BaseInfoItem struct { 129 IdCardNumber string `db:"id_card_number" json:"idCardNumber" field:"id_card_number"` 130 IsHouseholder bool `db:"is_householder" json:"isHouseholder" field:"is_householder"` 131 HouseholderRelation string `db:"householder_relation" json:"householderRelation" field:"householder_relation"` 132 UserName string `db:"user_name" json:"userName" field:"user_name"` 133 UserSex string `db:"user_sex" json:"userSex" field:"user_sex"` 134 UserAge int `db:"user_age" json:"userAge" field:"user_age"` 135 UserNation string `db:"user_nation" json:"userNation" field:"user_nation"` 136 } 137 138 type UserCollectionAddReq struct { 139 BaseInfo []BaseInfoItem `db:"_" json:"baseInfo" field:"_"` 140 } 141 jsonContent := `{ 142 "baseInfo": [{ 143 "idCardNumber": "520101199412141111", 144 "isHouseholder": true, 145 "householderRelation": "户主", 146 "userName": "李四", 147 "userSex": "男", 148 "userAge": 32, 149 "userNation": "苗族", 150 "userPhone": "13084183323", 151 "liveAddress": {}, 152 "occupationInfo": [{ 153 "occupationType": "经商", 154 "occupationBusinessInfo": [{ 155 "occupationClass": "制造业", 156 "businessLicenseNumber": "32020000012300", 157 "businessName": "土灶柴火鸡", 158 "spouseName": "", 159 "spouseIdCardNumber": "", 160 "businessLicensePhotoId": 125, 161 "businessPlace": "租赁房产", 162 "hasGoodsInsurance": true, 163 "businessScopeStr": "柴火鸡;烧烤", 164 "businessAddress": {}, 165 "businessPerformAbility": { 166 "businessType": "服务业", 167 "businessLife": 5, 168 "salesRevenue": 8000, 169 "familyEquity": 6000 170 } 171 }], 172 "occupationWorkInfo": { 173 "occupationClass": "", 174 "companyName": "", 175 "companyType": "", 176 "workYearNum": 0, 177 "spouseName": "", 178 "spouseIdCardNumber": "", 179 "spousePhone": "", 180 "spouseEducation": "", 181 "spouseCompanyName": "", 182 "workLevel": "", 183 "workAddress": {}, 184 "workPerformAbility": { 185 "familyAnnualIncome": 0, 186 "familyEquity": 0, 187 "workCooperationState": "", 188 "workMoneyCooperationState": "" 189 } 190 }, 191 "occupationAgricultureInfo": [] 192 }], 193 "assetsInfo": [], 194 "expenditureInfo": [], 195 "incomeInfo": [], 196 "liabilityInfo": [] 197 }] 198 }` 199 data := new(UserCollectionAddReq) 200 j, err := gjson.LoadJson(jsonContent, true) 201 t.AssertNil(err) 202 err = j.Scan(data) 203 t.AssertNil(err) 204 }) 205 } 206 207 func Test_Struct(t *testing.T) { 208 gtest.C(t, func(t *gtest.T) { 209 type Item struct { 210 Title string `json:"title"` 211 Key string `json:"key"` 212 } 213 214 type M struct { 215 Id string `json:"id"` 216 Me map[string]interface{} `json:"me"` 217 Txt string `json:"txt"` 218 Items []*Item `json:"items"` 219 } 220 221 txt := `{ 222 "id":"88888", 223 "me":{"name":"mikey","day":"20009"}, 224 "txt":"hello", 225 "items":null 226 }` 227 228 j, err := gjson.LoadContent(txt) 229 t.AssertNil(err) 230 t.Assert(j.Get("me.name").String(), "mikey") 231 t.Assert(j.Get("items").String(), "") 232 t.Assert(j.Get("items").Bool(), false) 233 t.Assert(j.Get("items").Array(), nil) 234 m := new(M) 235 err = j.Scan(m) 236 t.AssertNil(err) 237 t.AssertNE(m.Me, nil) 238 t.Assert(m.Me["day"], "20009") 239 t.Assert(m.Items, nil) 240 }) 241 } 242 243 func Test_Struct_Complicated(t *testing.T) { 244 type CertInfo struct { 245 UserRealName string `json:"userRealname,omitempty"` 246 IdentType string `json:"identType,omitempty"` 247 IdentNo string `json:"identNo,omitempty"` 248 CompanyName string `json:"companyName,omitempty"` 249 Website string `json:"website,omitempty"` 250 RegisterNo string `json:"registerNo,omitempty"` 251 AreaCode string `json:"areaCode,omitempty"` 252 Address string `json:"address,omitempty"` 253 CommunityCreditCode string `json:"communityCreditCode,omitempty"` 254 PhoneNumber string `json:"phoneNumber,omitempty"` 255 AreaName string `json:"areaName,omitempty"` 256 PhoneAreaCode string `json:"phoneAreaCode,omitempty"` 257 OperateRange string `json:"operateRange,omitempty"` 258 Email string `json:"email,omitempty"` 259 LegalPersonName string `json:"legalPersonName,omitempty"` 260 OrgCode string `json:"orgCode,omitempty"` 261 BusinessLicense string `json:"businessLicense,omitempty"` 262 FilePath1 string `json:"filePath1,omitempty"` 263 MobileNo string `json:"mobileNo,omitempty"` 264 CardName string `json:"cardName,omitempty"` 265 BankMobileNo string `json:"bankMobileNo,omitempty"` 266 BankCode string `json:"bankCode,omitempty"` 267 BankCard string `json:"bankCard,omitempty"` 268 } 269 270 type CertList struct { 271 StatusCode uint `json:"statusCode,string"` 272 SrcType uint `json:"srcType,string"` 273 CertID string `json:"certId"` 274 CardType string `json:"cardType,omitempty"` 275 CertInfo CertInfo `json:"certInfo"` 276 } 277 278 type Response struct { 279 UserLevel uint `json:"userLevel,string,omitempty"` 280 CertList []CertList `json:"certList"` 281 } 282 283 gtest.C(t, func(t *gtest.T) { 284 jsonContent := `{ 285 "certList":[ 286 {"certId":"2023313","certInfo":"{\"address\":\"xxxxxxx\",\"phoneNumber\":\"15084890\",\"companyName\":\"dddd\",\"communityCreditCode\":\"91110111MBE1G2B\",\"operateRange\":\"fff\",\"registerNo\":\"91110111MA00G2B\",\"legalPersonName\":\"rrr\"}","srcType":"1","statusCode":"2"}, 287 {"certId":"2023314","certInfo":"{\"identNo\":\"342224196507051\",\"userRealname\":\"xxxx\",\"identType\":\"01\"}","srcType":"8","statusCode":"0"}, 288 {"certId":"2023322","certInfo":"{\"businessLicense\":\"91110111MA00BE1G\",\"companyName\":\"sssss\",\"communityCreditCode\":\"91110111MA00BE1\"}","srcType":"2","statusCode":"0"} 289 ] 290 }` 291 j, err := gjson.LoadContent(jsonContent) 292 t.AssertNil(err) 293 var response = new(Response) 294 err = j.Scan(response) 295 t.AssertNil(err) 296 t.Assert(len(response.CertList), 3) 297 t.Assert(response.CertList[0].CertID, 2023313) 298 t.Assert(response.CertList[1].CertID, 2023314) 299 t.Assert(response.CertList[2].CertID, 2023322) 300 t.Assert(response.CertList[0].CertInfo.PhoneNumber, "15084890") 301 t.Assert(response.CertList[1].CertInfo.IdentNo, "342224196507051") 302 t.Assert(response.CertList[2].CertInfo.BusinessLicense, "91110111MA00BE1G") 303 }) 304 }