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条)

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站