php小編西瓜在這裡帶來了一個關於如何將JSON資料規範化為Go中的API結構體的精簡指南。在現代Web應用程式中,與JSON資料打交道是常見的任務。 Go語言作為一門強大的後端語言,提供了一種簡潔而靈活的方式來處理JSON資料。本文將介紹如何使用Go語言中的結構體來規範化JSON數據,從而更好地處理和操作它們。無論你是初學者還是有經驗的開發人員,本文都將為你提供有用的技巧和實用的範例。讓我們開始吧!
我對 go 還很陌生,正在嘗試確定是否有更簡潔的方法來完成從前端 (js) 到我的 api 的 json 資料的規範化。為了確保在從結構 (model.expense) 建立變數時使用正確的類型,我將有效負載轉儲到映射中,然後標準化並保存回結構。如果有人可以教我更好的方法來處理這個問題,我將不勝感激!先致謝!
模型.費用結構:
type expense struct { id primitive.objectid `json:"_id,omitempty" bson:"_id,omitempty"` name string `json:"name"` frequency int `json:"frequency"` startdate *time.time `json:"startdate"` enddate *time.time `json:"enddate,omitempty"` cost primitive.decimal128 `json:"cost"` paid []string `json:"paid,omitempty"` }
有問題的控制器:
func InsertOneExpense(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") w.Header().Set("Allow-Control-Allow-Methods", "POST") var expense map[string]interface{} json.NewDecoder(r.Body).Decode(&expense) var expenseName string if name, ok := expense["name"]; ok { expenseName = fmt.Sprintf("%v", name) } else { json.NewEncoder(w).Encode("missing required name") } var expenseFrequency int if frequency, ok := expense["frequency"]; ok { expenseFrequency = int(frequency.(float64)) } else { expenseFrequency = 1 } // Handle startDate normalization var expenseStartDate *time.Time if startDate, ok := expense["startDate"]; ok { startDateString := fmt.Sprintf("%v", startDate) startDateParsed, err := time.Parse("2006-01-02 15:04:05", startDateString) if err != nil { log.Fatal(err) } expenseStartDate = &startDateParsed } else { json.NewEncoder(w).Encode("missing required startDate") } // Handle endDate normalization var expenseEndDate *time.Time if endDate, ok := expense["endDate"]; ok { endDateString := fmt.Sprintf("%v", endDate) endDateParsed, err := time.Parse("2006-01-02 15:04:05", endDateString) if err != nil { log.Fatal(err) } expenseEndDate = &endDateParsed } else { expenseEndDate = nil } // Handle cost normaliztion var expenseCost primitive.Decimal128 if cost, ok := expense["cost"]; ok { costString := fmt.Sprintf("%v", cost) costPrimitive, err := primitive.ParseDecimal128(costString) if err != nil { log.Fatal(err) } expenseCost = costPrimitive } else { json.NewEncoder(w).Encode("missing required cost") return } normalizedExpense := model.Expense{ Name: expenseName, Frequency: expenseFrequency, StartDate: expenseStartDate, EndDate: expenseEndDate, Cost: expenseCost, } // Do more things with the struct var... }
您可以定義 json.unmarshaljson
接口,然後根據需要手動驗證資料。嘗試這樣的事情:
package main import ( "encoding/json" "fmt" "strconv" ) type CoolStruct struct { MoneyOwed string `json:"money_owed"` } // UnmarshalJSON the json package will delegate deserialization to our code if we implement the json.UnmarshalJSON interface func (c *CoolStruct) UnmarshalJSON(data []byte) error { // get the body as a map[string]*[]byte raw := map[string]*json.RawMessage{} if err := json.Unmarshal(data, &raw); err != nil { return fmt.Errorf("unable to unmarshal raw meessage map: %w", err) } // if we don't know the variable type sent we can unmarshal to an interface var tempHolder interface{} err := json.Unmarshal(*raw["money_owed"], &tempHolder) if err != nil { return fmt.Errorf("unable to unmarshal custom value from raw message map: %w", err) } // the unmarshalled interface has an underlying type use go's typing // system to determine type conversions / normalizations required switch tempHolder.(type) { case int64: // once we determine the type of the we just assign the value // to the receiver's field c.MoneyOwed = strconv.FormatInt(tempHolder.(int64), 10) // we could list all individually or as a group; driven by requirements case int, int32, float32, float64: c.MoneyOwed = fmt.Sprint(tempHolder) case string: c.MoneyOwed = tempHolder.(string) default: fmt.Printf("missing type case: %T\n", tempHolder) } // success; struct is now populated return nil } func main() { myJson := []byte(`{"money_owed": 123.12}`) cool := CoolStruct{} // outside of your struct you marshal/unmarshal as normal if err := json.Unmarshal(myJson, &cool); err != nil { panic(err) } fmt.Printf("%+v\n", cool) }
輸出:{moneyowed:123.12}
遊樂場連結:https://www.php.cn/link/87ca4eb840b6f78e3b6d6b418c0fef40
以上是如何最好地將 JSON 資料規範化為 Go 中的 API 結構體的詳細內容。更多資訊請關注PHP中文網其他相關文章!