dongqiulei6805 2016-01-10 20:21
浏览 36
已采纳

json解组通用接口{}

http://blog.golang.org/json-and-go

m := j.(map[string]interface{}) didn't work for me

panic: interface conversion: interface {} is []interface {}, not map[string]interface {}

So I end up with code like this to make it work? Don't think its the correct way of doing it :)

    var j interface{}
    err = json.Unmarshal(b, &j)
    if err != nil {
        log.Print(err.Error())
    }

    m := j.([]interface{}) //map[string]interface{}

    for k, v := range m {
        switch vv := v.(type) {
        case string:
            fmt.Println(k, "is string", vv)
        case int:
            fmt.Println(k, "is int", vv)
        case []interface{}:
            fmt.Println(k, "is an array:")
            for i, u := range vv {
                fmt.Println(i, u)
            }
        case map[string]interface{}:
            for k2, v2 := range v.(map[string]interface{}) {
                switch vv2 := v2.(type) {
                case string:
                    fmt.Println(k2, "is string", vv2)
                case int:
                    fmt.Println(k2, "is int", vv2)
                case []interface{}:
                    fmt.Println(k2, "is an array:")
                    for i2, u2 := range vv2 {
                        fmt.Println(i2, u2)
                    }
                default:
                    fmt.Println(k2, "is of a type I don't know how to handle")
                }
            }
        default:
            fmt.Println(k, "is of a type I don't know how to handle")
        }
    }

EDIT: My failed attempt to make it readable

    var j interface{}
    err = json.Unmarshal(b, &j)
    if err != nil {
        log.Print(err.Error())
    }
    write(j.([]interface{}))

func write(j []interface{}) {
    for k, v := range j {
        switch vv := v.(type) {
        case string:
            fmt.Println(k, "is string", vv)
        case int:
            fmt.Println(k, "is int", vv)
        case []interface{}:
            fmt.Println(k, "is an array:")
            for i, u := range vv {
                fmt.Println(i, u)
            }
        case map[string]interface{}:
            write(v.([]interface{}))
        default:
            fmt.Println(k, "is of a type I don't know how to handle")
        }
    }
}

EDIT2: Works but still ugly

    var j interface{}
    err = json.Unmarshal(b, &j)
    if err != nil {
        log.Print(err.Error())
    }
    write(j.([]interface{}))

func write(j []interface{}) {
    for k, v := range j {
        switch vv := v.(type) {
        case string:
            fmt.Println(k, "is string", vv)
        case int:
            fmt.Println(k, "is int", vv)
        case []interface{}:
            fmt.Println(k, "is an array:")
            for i, u := range vv {
                fmt.Println(i, u)
            }
        case map[string]interface{}:
            write2(v.(map[string]interface{}))
        default:
            fmt.Println(k, "is of a type I don't know how to handle")
        }
    }
}

func write2(j map[string]interface{}) {
    for k, v := range j {
        switch vv := v.(type) {
        case string:
            fmt.Println(k, "is string", vv)
        case int:
            fmt.Println(k, "is int", vv)
        case []interface{}:
            fmt.Println(k, "is an array:")
            for i, u := range vv {
                fmt.Println(i, u)
            }
        default:
            fmt.Println(k, "is of a type I don't know how to handle")
        }
    }
}
  • 写回答

1条回答 默认 最新

  • dpcyx08288 2016-01-11 00:08
    关注

    It seems as the root part of the JSON code is a JSON array (the ["item", "item", {"key": "value"}] notation). Although some JSON libraries don't like it, the "root" of the JSON blob can be an array and not an object (the {"key": "value"} notation).

    In a production application, you should use the second value of type assertions to make sure it's the right type:

    m, mOk := j.(map[string]interface{})
    s, sOk := j.([]interface{})
    
    if mOk {
        // Use Map
    } else {
        // Use Slice
    }
    

    If you don't need to edit the data, Jason is a great library for Go.

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

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么