为了账号安全,请及时绑定邮箱和手机立即绑定

动态创建结构体

动态创建结构体

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 关注
  • 90 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信