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`))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮