亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

動態創建結構體

動態創建結構體

Go
PIPIONE 2023-08-07 14:45:51
我想使用 API 以 json 方式與應用程序進行通信。該應用程序有一個包含該字段的結構:CustomFields interface{} `json:"custom_fields,omitempty"`當調用 API 時,這部分可能是這樣的:"custom_fields": {  "Field1": "Value1",  "Field2": "Value2"}自定義字段的字段名稱不固定。它們可以在應用程序中設置。所以我不能只創建一個結構。我需要在代碼中動態構建結構。是否可以?
查看完整描述

3 回答

?
RISEBY

TA貢獻1856條經驗 獲得超5個贊

構建 JSON 時,您可以使用該interface{}變量的任何結構:

data.CustomFields=myStructVar

上面的myStructVar是可以編組為 JSON 的任何結構。

如果您沒有自定義字段的結構,您可以使用map[string]interface{}

data.CustomFields=map[string]interface{}{"field1":"value1","field2":"value2"}

當您解組 JSON 輸入時,interface{}自定義字段將被解組為map[string]interface{}JSON 對象、[]interface{}JSON 數組或原始值之一(string、float64、bool)。



查看完整回答
反對 回復 2023-08-07
?
UYOU

TA貢獻1878條經驗 獲得超4個贊

不要對自定義字段使用空接口,而是使用map[string]interface{}.


type Payload struct {

    Field1 string `json:"field1"`

    Field2 int `json:"field2"`

    CustomFields map[string]interface `json:"customFields,omitempty`

}

完整的工作示例


查看完整回答
反對 回復 2023-08-07
?
月關寶盒

TA貢獻1772條經驗 獲得超5個贊

我做了這段代碼...

它似乎無法正常工作,我做錯了什么?


tenantCustomFields := d.Get("custom_field").(*schema.Set).List()               


  cf := make(map[string]interface{})                                             

  for _, customFieldRaw := range tenantCustomFields {                            

    customField := customFieldRaw.(map[string]interface{})                         

    customFieldName := customField["name"].(string)                                

    customFieldType := customField["type"].(string)                                

    customFieldValue := customField["value"].(string)                              


    if customFieldType == "string" {                                               

      cf[customFieldName] = customFieldValue                                         

    } else if customFieldType == "integer" {                                       

      cfIntValue, err := strconv.ParseInt(customFieldValue, 10, 64)                  

      if err == nil {                                                                

        return err                                                                     

      }                                                                              

      cf[customFieldName] = cfIntValue                                               

    } else if customFieldType == "boolean" {                                       

      cfBoolValue, err := strconv.ParseBool(customFieldValue)                        

      if err == nil {                                                                

        return err                                                                     

      }                                                                              

      cf[customFieldName] = cfBoolValue                                              

    }                                                                              

  }


查看完整回答
反對 回復 2023-08-07
  • 3 回答
  • 0 關注
  • 180 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號