###### tags: `Golang Package` # GoJson ![](https://i.imgur.com/R5gCULo.png =300x300) [github](https://github.com/ChimeraCoder/gojson) 將Json格式轉換為Golang結構 ## Usecase 1 ```go= var b []byte i := strings.NewReader(`{"foo" : "bar", "baz" : 123}`) b, err := gojson.Generate(i, gojson.ParseJson, "TestStruct", "gojson", []string{"json"}, false, true) if err != nil { t.Error("Generate() error:", err) } ioutil.WriteFile("gojsonFile", b, 0644) ``` 產生以下檔案 ```go= package gojson type TestStruct struct { Baz int64 `bson:"baz"` Foo string `bson:"foo"` } ``` ## Usecase 2 當欄位有可能為null時 資源 ```json= { "item":"19 黑金", "price": 152000, "buyer":null } ``` 程式碼 ```go= var b []byte data, _ := ioutil.ReadFile("./test.json") i := strings.NewReader(string(data)) b, err := gojson.Generate(i, gojson.ParseJson, "TestStruct", "main", []string{"bson", "json"}, true, true) if err != nil { t.Error("Generate() error:", err) } ioutil.WriteFile("gojsonStruct.go", b, 0644) ``` 產生 ```go= package main type TestStruct struct { Buyer interface{} `bson:"buyer" json:"buyer"` Item string `bson:"item" json:"item"` Price int64 `bson:"price" json:"price"` } ``` ## Usecase 3 有數個子結構 ```json= { "item":"19 黑金", "price": 152000, "buyer":{ "name":"Mask", "age":23, "status":1 }, "itemStatus":{ "hot":1293.2, "watching":20931, "add":20, "customer":{ "count":123, "name":"熟客", "value":100, "member":["mask","adam","gary","daniel","james"] } } } ``` 產生結構 ```go= package main type TestStruct struct { Buyer TestStruct_sub1 `bson:"buyer" json:"buyer"` Item string `bson:"item" json:"item"` ItemStatus TestStruct_sub3 `bson:"itemStatus" json:"itemStatus"` Price int64 `bson:"price" json:"price"` } type TestStruct_sub3 struct { Add int64 `bson:"add" json:"add"` Customer TestStruct_sub2 `bson:"customer" json:"customer"` Hot float64 `bson:"hot" json:"hot"` Watching int64 `bson:"watching" json:"watching"` } type TestStruct_sub1 struct { Age int64 `bson:"age" json:"age"` Name string `bson:"name" json:"name"` Status int64 `bson:"status" json:"status"` } type TestStruct_sub2 struct { Count int64 `bson:"count" json:"count"` Member []string `bson:"member" json:"member"` Name string `bson:"name" json:"name"` Value int64 `bson:"value" json:"value"` } ``` subStruct 為 false時,會用內嵌結構的方式產生 convertFloat 為flase時,會把所有數字欄位轉成float64 ```go= package main type TestStruct struct { Buyer struct { Age float64 `bson:"age" json:"age"` Name string `bson:"name" json:"name"` Status float64 `bson:"status" json:"status"` } `bson:"buyer" json:"buyer"` Item string `bson:"item" json:"item"` ItemStatus struct { Add float64 `bson:"add" json:"add"` Customer struct { Count float64 `bson:"count" json:"count"` Member []string `bson:"member" json:"member"` Name string `bson:"name" json:"name"` Value float64 `bson:"value" json:"value"` } `bson:"customer" json:"customer"` Hot float64 `bson:"hot" json:"hot"` Watching float64 `bson:"watching" json:"watching"` } `bson:"itemStatus" json:"itemStatus"` Price float64 `bson:"price" json:"price"` } ``` ## Item ```json= { "data": { "id": "5517320660080590848", "catalogId": "5515824272536698880", "info": { "name": "Palm Springs後背包 舊款", "brand": "Louis Vuitton", "brandId": "1358945132665114624", "quantity": 1, "condition": 95, "color": { "id": "1358945144086204416", "name": "品牌老花款", "hex": "", "imageUrl": "https://img.relithe.com/Category/color/60642e0a240643db53e84707.png?hash=LDRC[6t7%M~qM{WBxuD%xuWBRjIU", "selected": 2 }, "dimensions": "Mini", "price": 61000, "accessories": [ { "type": 1, "text": "" }, { "type": 3, "text": "" }, { "type": 5, "text": "" }, { "type": 7, "text": "商品卡 緞帶" } ], "volume": { "width": 9, "length": 15, "height": 22 }, "style": { "id": "1358945142504951808", "type": "包款", "name": "後背包" }, "tags": [], "material": [ "合成帆布", "亮金金屬", "皮革拼帆布", "自然紋牛皮" ], "delivery": [ 1, 2, 3 ], "brandAlias": [ "LV" ], "discountActivity": { "name": "", "endAt": "", "promoShare": 0, "type": 0, "content": 0, "price": 0, "enable": false } }, "imageUrl": { "main": [ { "url": "https://img.relithe.com/v160/BuyOrder/1365178880733745152/1385561646864273408.jpg?hash=LWQ0UBaf~qj[_3j[M_j[.8j[9Fay", "description": "" } ], "detail": [ { "url": "https://img.relithe.com/v160/BuyOrder/1365178880733745152/1385561950561243136.jpg?hash=LnMaO-9F~qofD%IUWBoe~qayD%of", "description": "" } ] }, "isLogin": false } } ``` ![](https://i.imgur.com/qTObrpu.png) ## Zerocard 主動查詢 ```json= { "info": [ { "transaction_state": "003", "order_id": "test20190806004", "spanapp_id": "EPA991908060005", "store": null, "reserve_date": "20190806", "transacting_date": "20190806", "product_name": "測試商品", "authorize_expire": "20191004", "disburse_date": null, "amount": 10000, "installment": 12, "fee_type": "vendor", "info_customer_json":"jrpx1Xb/bUmezoFxeaFnJZFHq3qHD3jIBO97XkcQ1S9GFkg5Y2m1oAsod+4kR3UGrCKd/5pdQKY5JGnsUUEufJ0oOnjZF20YoXVbaV27ylQ=", "refundlist": [ { "refund_time": null, "refund_amount": 0, "final_amount": 0, "refund_id": null } ] }, { "transaction_state": "007", "order_id": "test20190806005", "spanapp_id": "EPA991908060006", "store": null, "reserve_date": "20190806", "transacting_date": "20190806", "product_name": "測試商品", "authorize_expire": "20191004", "disburse_date": null, "amount": 10000, "installment": 12, "fee_type": "vendor", "info_customer_json": "", "refundlist": [ { "refund_time": "2019-08-06 12:43:00", "refund_amount": 10000, "final_amount": 0, "refund_id": "019080621032567" } ] } ], "result": "000", "result_message": "成功" } ``` ![](https://i.imgur.com/PZJ6nyg.png) # 結論 下次有要接第三方的時候可以試試看,省去寫結構的時間 {%hackmd theme-dark %}