golang json和界面片 [英] golang json and slices of interface

查看:162
本文介绍了golang json和界面片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这个问题是由于尝试使用返回JSON数据的API调用而产生的。返回的数据非常多,结构根据请求而显着不同。在API文档中也没有针对JSON响应的结构,因此我试图实现一些使用任意JSON响应的方法。



现在,当初始调用它被放入map [string] interface {}中,然后运行switch语句来确定每个元素的类型,遇到一些接口时会出现问题。我似乎无法对他们做任何事情。



我曾尝试过使用sort包几次(特别是sort和slicestable函数)无济于事。 p>

我收到的错误是:

 界面转换:界面{}是[] interface {},而不是map [string]接口{} 

映射接口片,以便我可以用switch语句重新遍历它们。

  output:= make(map [string]接口{})
err = json.Unmarshal(body,& output)

fmt.Println(err)
identify(输出)

返回err
}

func identify(输出map [string] interface {}){
fmt.Printf(%T,输出)
for a, b:=范围输出{
switch bb:= b。(type){
case string:
fmt.Println(This is a string)
case float64:
fmt.Println(这是一个浮动)
的情况[] interface {}:
fmt.Println(is [] interface,bb)
test:= b。(map [string] interface {})// falis here
fmt .Println(成功!,测试)
默认:
返回
}
}
}

所以基本的问题是,我怎么迭代嵌套的接口切片而不事先知道结构?

解决方案

您可以添加一个switch case来检查接口片的接口类型,然后运行与递归相同的函数,直到解析完整个json。

 输出:= make(map [string] interface {})
err = json.Unmarshal(body,& output)

fmt.Println(err)
identify(output)

return err
}

func identify(输出map [string] interface {} ){
fmt.Printf(%T,输出)
for a,b:=范围输出{
switch bb:= b。(type){
case st ring:
fmt.Println(This is a string)
case float64:
fmt.Println(this is a float)
case [] interface {}:
//访问JSON对象中的值,并将它们放置在项目
中用于_,itemValue:=范围jsonObj {
fmt.Printf(%v是一个接口\ n (itemValue。(map [string] interface {}))
}
default:
return
}
}
}

可以嵌套深层json。我们只需要为每个案例创建选项,直到json完全解析。

I am having trouble iterating over slices of interfaces that contain slices of interfaces.

This problem has arisen through attempting to work with an API call which returns JSON data. There is quite a lot of data returned and the structure differs quite dramatically depending on the request. There is also no structure for the JSON responses in the API documentation so I am trying to implement some methods for working with arbitrary JSON responses.

Presently when the initial call is made it is dropped into a map[string]interface{} and then a switch statement is run to determine the type of each element, the problem occurs when a slice of interfaces is encountered. I can't seem to do anything with them.

I have tried using the sort package a few times (specifically the sort and slicestable functions) to no avail.

The error I am receiving is:

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

Which occurs when I try and map the slice of interfaces so I can iterate over them with the switch statement again.

output := make(map[string]interface{})
err = json.Unmarshal(body, &output)

fmt.Println(err)
identify(output)

return err
}

func identify(output map[string]interface{}) {
    fmt.Printf("%T", output)
    for a, b := range output {
        switch bb := b.(type) {
        case string:
            fmt.Println("This is a string")
        case float64:
            fmt.Println("this is a float")
        case []interface{}:
            fmt.Println(" is []interface ", bb)
            test := b.(map[string]interface{}) // falis here
            fmt.Println("Success!", test)
        default:
            return
        }
    }
}

So the basic question is how do I iterate over nested slices of interfaces without knowing the structure beforehand?

解决方案

You can add a switch case which is checking the type of interface for slice of interfaces and then run the same function as recursive until whole json is parsed.

output := make(map[string]interface{})
err = json.Unmarshal(body, &output)

fmt.Println(err)
identify(output)

return err
}

func identify(output map[string]interface{}) {
    fmt.Printf("%T", output)
    for a, b := range output {
        switch bb := b.(type) {
        case string:
            fmt.Println("This is a string")
        case float64:
            fmt.Println("this is a float")
        case []interface{}:
        // Access the values in the JSON object and place them in an Item
        for _, itemValue := range jsonObj {
            fmt.Printf("%v is an interface\n", itemValue)
            identify(itemValue.(map[string]interface{}))
        }
        default:
            return
        }
    }
}

There can be deep nested json. We just needs to create options for each case until json is completely parsed.

这篇关于golang json和界面片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆