dtlygweb2017 2018-04-30 00:02
浏览 50
已采纳

如何解组可以是字符串*或*字符串数组的不一致的JSON字段?

I am having trouble Unmarshalling some Json I don't have control over. There is one field that 99% of the time is a string but occasionally is an array.

type MyListItem struct {
    Date  string `json:"date"`
    DisplayName       string `json:"display_name"`
}

type MyListings struct {
    CLItems []MyListItem `json:"myitems"`
}

var mylist MyListings
err = json.Unmarshal(jsn, &mylist)
if err != nil {
    fmt.Print("JSON:
%s
 error:%v
", string(jsn),err)
    return
}

Json is as follows:

{       
    "date": "30 Apr",
    "display_name": "Mr Smith"
},
{
    "date": "30 Apr",
    "display_name": ["Mr Smith", "Mr Jones"],
}

error: json: cannot unmarshal array into Go struct field MyListItem.display_name of type string

  • 写回答

2条回答 默认 最新

  • dongxunhua2054 2018-04-30 00:39
    关注

    Use json.RawMessage to capture the varying field.

    Use the json "-" name to hide the DisplayName field from decoder. The application will fill this field after the top-level JSON is decoded.

    type MyListItem struct {
        Date           string          `json:"date"`
        RawDisplayName json.RawMessage `json:"display_name"`
        DisplayName    []string        `json:"-"`
    }
    

    Unmarshal the top-level JSON:

    var li MyListItem
    if err := json.Unmarshal(data, &li); err != nil {
        // handle error
    }
    

    Unmarshal the display name depending on the type of the raw data:

    if len(li.RawDisplayName) > 0 {
        switch li.RawDisplayName[0] {
        case '"':
            if err := json.Unmarshal(li.RawDisplayName, &li.DisplayName); err != nil {
                // handle error
            }
        case '[':
            var s []string
            if err := json.Unmarshal(li.RawDisplayName, &s); err != nil {
                // handle error
            }
            // Join arrays with "&" per OP's comment on the question.
            li.DisplayName = strings.Join(s, "&")
        }
    }
    

    playground example

    Incorporate the above into a for loop to handle MyListings:

    var listings MyListings
    if err := json.Unmarshal([]byte(data), &listings); err != nil {
        // handle error
    }
    for i := range listings.CLItems {
        li := &listings.CLItems[i]
        if len(li.RawDisplayName) > 0 {
            switch li.RawDisplayName[0] {
            case '"':
                if err := json.Unmarshal(li.RawDisplayName, &li.DisplayName); err != nil {
                    // handle error
                }
            case '[':
                var s []string
                if err := json.Unmarshal(li.RawDisplayName, &s); err != nil {
                    // handle error
                }
                li.DisplayName = strings.Join(s, "&")
            }
        }
    }
    

    playground example

    If there's more than one place in the data model where a value can be a string or []string, it can be helpful to encapsulate the logic in a type. Parse the JSON data in an implementation of the json.Unmarshaler interface.

    type multiString string
    
    func (ms *multiString) UnmarshalJSON(data []byte) error {
        if len(data) > 0 {
            switch data[0] {
            case '"':
                var s string
                if err := json.Unmarshal(data, &s); err != nil {
                    return err
                }
                *ms = multiString(s)
            case '[':
                var s []string
                if err := json.Unmarshal(data, &s); err != nil {
                    return err
                }
                *ms = multiString(strings.Join(s, "&"))
            }
        }
        return nil
    }
    

    Use it like this:

    type MyListItem struct {
        Date        string      `json:"date"`
        DisplayName multiString `json:"display_name"`
    }
    
    type MyListings struct {
        CLItems []MyListItem `json:"myitems"`
    }
    
    var listings MyListings
    if err := json.Unmarshal([]byte(data), &listings); err != nil {
        log.Fatal(err)
    }
    

    Playground Example

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function