dongyou8368
dongyou8368
2019-04-16 18:31

如何解组包含整数,浮点数和数字字符串的JSON?

已采纳

I have a multiple different JSON requests of data that is being passed into my Go app that contains numbers in different formats. An example of a request is as follows:

{
    "stringData":"123456",
    "intData": 123456,
    "floatData": 123456.0
}

Is there a way to unmarshal this data into the type which is determined by the JSON data. For example, string data would be "123456", int data would be 123456 and float data would be 123456.0. I do not have structs defined for these JSON objects and creating structs for these are not an option.

I have looked at the decoder.UseNumber() method to convert the data into strings, but I don't know how to handle the difference between stringData and intData after that.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • doujiong9915 doujiong9915 2年前

    Decode to map[string]interface{} with the UseNumber option. Use a type assertion to find numbers and convert based on presence of the ".".

    dec := json.NewDecoder(r)
    dec.UseNumber()
    
    var m map[string]interface{}
    err := dec.Decode(&m)
    if err != nil {
        log.Fatal(err)
    }
    
    for k, v := range m {
        v, err := decodeValue(v)
        if err != nil {
            log.Fatal(err)
        }
    for k, v := range m {
        v, err := decodeValue(v)
        if err != nil {
            log.Fatal(err)
        }
        switch v := v.(type) {
        case string:
            fmt.Printf("%s is a string with value %q
    ", k, v)
        case int64:
            fmt.Printf("%s is a integer with value %d
    ", k, v)
        case float64:
            fmt.Printf("%s is a float with value %f
    ", k, v)
        default:
            fmt.Printf("%s is a %T with value %v
    ", k, v, v)
        }
    }
    
    
    ...
    
    func decodeValue(v interface{}) (interface{}, error) {
        if vv, ok := v.(json.Number); ok {
            if strings.Contains(vv.String(), ".") {
                return vv.Float64()
            } else {
                return vv.Int64()
            }
        } else {
            return v, nil
        }
    }
    

    Run it on the playground.

    This example prints what's found and exits the program on error. If your goal is to create a map with the values of the correct types, then replace the code that prints numbers with m[k] = n.

    点赞 评论 复制链接分享
  • drqn5418 drqn5418 2年前

    You can unmarshal the json to a map[string]interface{} and then cast to the correct types using a type switch.

    点赞 评论 复制链接分享