doupo2241 2017-05-18 21:20
浏览 45
已采纳

前往:将JSON解编为多种类型

I'm having an issue unmarshalling a JSON response into a struct. The problem I'm having is that the zip code can either return as a string or an integer. How do I write an unmarshal method to check if the zip is an int and force it to store it as a string?

Struct:

type CustomerAddress struct {
    Line1            string `json:"line1"`
    City             string `json:"city"`
    State            string `json:"state"`
    Zip              string `json:"zip"`
    IsPrimaryAddress string `json:"isPrimaryAddress"`
}

Example Json:

address": [
  {
    "line1": "555 ADDRESS PLACE",
    "city": "DALLAS",
    "state": "TX",
    "isPrimaryAddress": "Y",
    "zip": 55555
  }
]

After unmarshalling, the result should have the zip successfully converted into a string:

address": [
  {
    "line1": "555 ADDRESS PLACE",
    "city": "DALLAS",
    "state": "TX",
    "isPrimaryAddress": "Y",
    "zip": "55555"
  }
]

As an attempt, I tried to use a ZipWrapper.

type CustomerAddress struct {
    Line1            string        `json:"line1"`
    City             string        `json:"city"`
    State            string        `json:"state"`
    Zip              ZipWrapper    `json:"zip"`
    IsPrimaryAddress string        `json:"isPrimaryAddress"`
}

type ZipWrapper struct {
   Zip string
}

func (w *ZipWrapper ) UnmarshalJSON(data []byte) (err error) {

    if zip, err := strconv.Atoi(string(data)); err == nil {
        w.Zip = strconv.Itoa(zip)
        return nil
    }
    return json.Unmarshal(data, &w.Zip)
}

This almost worked except the zip is now a nested struct within CustomerAddress which is not what I want:

  address": [
  {
    "line1": "555 ADDRESS PLACE",
    "city": "DALLAS",
    "state": "TX",
    "isPrimaryAddress": "Y",
    "zip": {
      "Zip": "55555"
    }
  }
]

Any ideas? I feel like this is a relatively easy task but I'm a complete Go noob and haven't fully wrapped my head around how Unmarshalling works.

  • 写回答

2条回答 默认 最新

  • 普通网友 2017-05-18 21:26
    关注

    The json package provides the json.Number type to do this:

    type CustomerAddress struct {
        Line1            string      `json:"line1"`
        City             string      `json:"city"`
        State            string      `json:"state"`
        Zip              json.Number `json:"zip"`
        IsPrimaryAddress string      `json:"isPrimaryAddress"`
    }
    

    https://play.golang.org/p/PIKSh2c6Mm

    If you needed to do this yourself without the nested struct, you can declare the type the same way as json.Number, with string as the underlying type

    type ZipWrapper string
    
    func (w *ZipWrapper) UnmarshalJSON(data []byte) (err error) {
        if len(data) > 1 && data[0] == '"' && data[len(data)-1] == '"' {
            data = data[1 : len(data)-1]
        }
    
        if _, err := strconv.Atoi(string(data)); err != nil {
            return err
        }
        *w = ZipWrapper(string(data))
        return nil
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么