dsgdfh302506 2018-02-13 16:58
浏览 35
已采纳

将JSON解组为最小类型

I have a Json string that I want to unmarshal. This is working:

jsonString := []byte(`{"my_int": 3, "my_string": null}`)
var data map[string]interface{}
err := json.Unmarshal(jsonString, &data)
if err != nil {
    fmt.Println(err)
}
//avroJson := make(map[string]interface{})
for k, v := range data {
    fmt.Printf("%v, %T
", k, v)
}

My issue is: the value of my_int which is 3 is returned as float64.

My question is: how to parse a json string with the "minimum type" so that 3 will return int32 and not the maximum type 3 => float64?

Assumption: my Json is huge and only have primitive types and I want a minimum value that is really float64 to continue to show float64.

Clarification: A "minimum type" means that if 3 can be considered both int32 and float64 the "minimum type" will be int32, which is the exact type you'll get when running this: reflect.TypeOf(3).string()

  • 写回答

2条回答 默认 最新

  • dphj737575 2018-02-14 14:43
    关注

    Since you cannot describe your data in a struct then your options are to:

    1. Use a json.Decoder to convert the values to your desired types as they are parsed.

    2. Parse the document into a generic interface and post-process the value types.

    Option #1 is the most flexible and can likely be implemented to be more performant than the other option since parsing and transformation could be performed in a single pass of the data.

    Option #2 might be simpler but will require two passes over the data. Here is an example of what the post-processing step might look like:

    func TransformValueTypes(o map[string]interface{}) {
      for k, v := range o {
        // Convert nil values to *string type.
        if v == interface{}(nil) {
          o[k] = (*string)(nil)
        }
        // Convert numbers to int32 if possible
        if x, isnumber := v.(float64); isnumber {
          if math.Floor(x) == x {
            if x >= math.MinInt32 && x <= math.MaxInt32 {
              o[k] = int32(x)
            }
            // Possibly check for other integer sizes here?
          }
          // Possibly check if float32 is possible here?
        }
        // Check for maps and slices here...
      }
    }
    

    So if you call TransformValueTypes(data) then your types will look like:

    // my_int     -> 3     (int32)
    // my_string  -> <nil> (*string)
    // my_string2 -> "foo" (string)
    // my_float   -> 1.23  (float64)
    

    Of course, your transform function could also apply type transformation logic based on the key name.

    Importantly, note that if your document might have additional structure not mentioned in your question (such as nested objects or arrays) then your transform function will need to account for them by more value type checking, recursive calls, and iteration.

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

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题