du229908 2015-07-25 10:30
浏览 238
已采纳

如何在Go中解析纯数字和加引号的JSON数字?

I'm dealing with a third-party JSON-based API. Usually it wraps all numbers in quotes, but sometimes it doesn't. Nothing I can do about it.

I'm trying to come up with a solution, which parses the numbers regardless of whether they're enquoted or not. Standard library provides a ,string field tag, which allows to map numeric fields to enquoted values, but, unfortunately, it then fails to process the value, if it's not in quotes.

func test(s string) {
  err := json.Unmarshal([]byte(s), &struct {
    F1 float64
    F2 float64 `json:",string"`
  }{})
  if err != nil {
    fmt.Println(err)
    return
  }

  fmt.Println("success")
}

func main() {
  test(`{"f1": 1.23}`)   // success
  test(`{"f1": "1.23"}`) // cannot unmarshal string into Go value of type float64
  test(`{"f2": 1.23}`)   // invalid use of ,string struct tag, trying to unmarshal unquoted value into ...
  test(`{"f2": "1.23"}`) // success
}

The Go Playground

Is there a way around this?

  • 写回答

1条回答 默认 最新

  • dstd2129 2015-07-25 10:30
    关注

    The proper solution is to "clone" float64 and define custom MarshalJSON and UnmarshalJSON for it:

    type jsonFloat64 float64
    
    func (f jsonFloat64) MarshalJSON() ([]byte, error) {
      return json.Marshal(float64(f))
    }
    
    func (f *jsonFloat64) UnmarshalJSON(data []byte) error {
      if len(data) >= 2 && data[0] == '"' && data[len(data)-1] == '"' {
        data = data[1 : len(data)-1]
      }
    
      var tmp float64
      err := json.Unmarshal(data, &tmp)
      if err != nil {
        return err
      }
    
      *f = jsonFloat64(tmp)
      return nil
    }
    

    Then you'd be able to do something like this:

    func test(s string) {
      err := json.Unmarshal([]byte(s), &struct {
        F jsonFloat64
      }{})
      if err != nil {
        fmt.Println(err)
        return
      }
    
      fmt.Println("success")
    }
    
    func main() {
      test(`{"f": 1.23}`)   // success
      test(`{"f": "1.23"}`) // success
    }
    

    The Go Playground

    Feel free to adjust UnmarshalJSON to your needs, mine is pretty strict about the spacing. Credit goes to Dave C, this was heavily inspired by his comment on another question (which also features more variations on the solution above).

    Alternatively, you could pre-process the JSON data with a regular expression, but don't do this if the solution above is a viable option, it's much faster.

    re := regexp.MustCompile(`(":\s*)([\d\.]+)(\s*[,}])`)
    rawJsonByteArray = re.ReplaceAll(rawJsonByteArray, []byte(`$1"$2"$3`))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?