dongshi9526 2014-01-18 18:28
浏览 140
已采纳

将interface {}转换为特定类型

I am developing web service that will receive JSON. Go converts types too strict.

So I did following function to convert interface{} in bool

func toBool(i1 interface{}) bool {
    if i1 == nil {
        return false
    }
    switch i2 := i1.(type) {
    default:
        return false
    case bool:
        return i2
    case string:
        return i2 == "true"
    case int:
        return i2 != 0
    case *bool:
        if i2 == nil {
            return false
        }
        return *i2
    case *string:
        if i2 == nil {
            return false
        }
        return *i2 == "true"
    case *int:
        if i2 == nil {
            return false
        }
        return *i2 != 0
    }
    return false
}

I believe that function is still not perfect and I need functions to convert interface{} in string, int, int64, etc

So my question: Is there library (set of functions) in Go that will convert interface{} to certain types

UPDATE

My web service receive JSON. I decode it in map[string]interface{} I do not have control on those who encode it.

So all values I receive are interface{} and I need way to cast it in certain types.

So it could be nil, int, float64, string, [...], {...} and I wish to cast it to what it should be. e.g. int, float64, string, []string, map[string]string with handling of all possible cases including nil, wrong values, etc

UPDATE2

I receive {"s": "wow", "x":123,"y":true}, {"s": 123, "x":"123","y":"true"}, {a:["a123", "a234"]}, {}

var m1 map[string]interface{}
json.Unmarshal(b, &m1)
s := toString(m1["s"])
x := toInt(m1["x"])
y := toBool(m1["y"])
arr := toStringArray(m1["a"])
  • 写回答

1条回答 默认 最新

  • dongwen7187 2014-01-20 07:21
    关注

    objx package makes exactly what you want, it can work directly with JSON, and will give you default values and other cool features:

    Objx provides the objx.Map type, which is a map[string]interface{} that exposes a powerful Get method (among others) that allows you to easily and quickly get access to data within the map, without having to worry too much about type assertions, missing data, default values etc.

    This is a small example of the usage:

    o := objx.New(m1) 
    s := o.Get("m1").Str() 
    x := o.Get("x").Int() 
    y := o.Get("y").Bool()
    
    arr := objx.New(m1["a"])
    

    A example from doc working with JSON:

    // use MustFromJSON to make an objx.Map from some JSON
    m := objx.MustFromJSON(`{"name": "Mat", "age": 30}`)
    
    // get the details
    name := m.Get("name").Str()
    age := m.Get("age").Int()
    
    // get their nickname (or use their name if they
    // don't have one)
    nickname := m.Get("nickname").Str(name)
    

    Obviously you can use something like this with the plain runtime:

    switch record[field].(type) {
    case int:
        value = record[field].(int)
    case float64:
        value = record[field].(float64)
    case string:
        value = record[field].(string)
    }
    

    But if you check objx accessors you can see a complex code similar to this but with many case of usages, so i think that the best solution is use objx library.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 经gamit解算的cors站数据再经globk网平差得到的坐标做形变分析
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式