golang json和界面片

编程入门 行业动态 更新时间:2024-10-23 23:28:49
本文介绍了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和界面片

本文发布于:2023-07-30 00:39:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1245011.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:界面   golang   json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!