首页 > 后端开发 > Golang > 正文

在 Go Lang 中处理嵌套非结构化 JSON

WBOY
发布: 2024-02-11 22:36:09
转载
991 人浏览过

在 Go Lang 中处理嵌套非结构化 JSON

在Go Lang中处理嵌套非结构化JSON是一项关键任务。JSON(JavaScript Object Notation)是一种常用的数据交换格式,但当JSON数据嵌套复杂时,处理起来可能会变得困难。php小编鱼仔将为您介绍一些在Go Lang中处理嵌套非结构化JSON的方法和技巧,帮助您更高效地解析和操作这些数据。通过掌握这些技能,您将能够轻松处理复杂的JSON数据,提高代码的可读性和可维护性。

问题内容

我试图了解如何从 golang 中的非结构化 json 数据访问特定数据。我有以下 json,当 foo1 有一些与空的 foo2 不同的数据时,我尝试访问 material 下的“foo1”。当像 foo1 这样的对象有数据时,我还需要从同名的分类部分读取数据。例如。由于material部分下的foo1有数据,我应该已经打印material->foo1下的方法键值以及来自分类-> foo1的desc。

package main

import (
    "encoding/json"
    "fmt"
)


type new struct {
    desc string `json:"desc"`
}

func main() {
    bjson := `{ 
                "classifications": { "foo1": { "desc": "it may be possible.", "sol": "the backups.", "ref": { "sensitive information": "https://www.sensitive_information.html", "control sphere": "https://ww.example.org/data.html" },"bar1": { "desc": "the target", "sol": "should be used.", "ref": { "abc: srgery": "https://www.orp.org/" } }}, 
               
                "material": { "backup file": [],"foo1": [ { "method": "get", "info": "it is not set", "level": 1, "parameter": "", "referer": "", "module": "diq", "curl_command": "curl \"https://example.com/\"", "wsg": [ "conf-12", "o-policy" ] }],"foo2": [],"bar1": []},
         
                "anomalies": { "server error": [], "resource consumption": [] }, 
                
                "additionals": { "web technology": [], "methods": [] }, 

                "infos": { "url": "https://example.com/", "date": "thu, 08 dec 2022 06:52:04 +0000"}}}`


    
        var parseddata = make(map[string]map[string]new)
    json.unmarshal([]byte(bjson), &parseddata)
    fmt.println("output of parseddata - \n", parseddata["classifications"]["foo1"].desc)
    
    //for _, v := range parseddata["material"] {
    //  fmt.println(v)
    //}
}
登录后复制

如果 foo1 不为空,则预期输出:

Method is GET
desc is It may be possible.
登录后复制

解决方法

您可以将其解组到 map[string]interface{} 变量中,然后使用一系列类型断言来获取您想要的信息,例如:

var parseddata = make(map[string]interface{})
json.unmarshal([]byte(bjson), &parseddata)
fmt.printf("method is %s\n", parseddata["classifications"].
  (map[string]interface{})["material"].
  (map[string]interface{})["foo1"].
  ([]interface{})[0].
  (map[string]interface{})["method"].(string))
登录后复制

以上将输出:

method is get
登录后复制

这是完整的、可运行的代码版本:

package main

import (
    "encoding/json"
    "fmt"
)

type new struct {
    desc string `json:"desc"`
}

func main() {
    bjson := `{ 
                "classifications": { "foo1": { "desc": "it may be possible.", "sol": "the backups.", "ref": { "sensitive information": "https://www.sensitive_information.html", "control sphere": "https://ww.example.org/data.html" },"bar1": { "desc": "the target", "sol": "should be used.", "ref": { "abc: srgery": "https://www.orp.org/" } }}, 
               
                "material": { "backup file": [],"foo1": [ { "method": "get", "info": "it is not set", "level": 1, "parameter": "", "referer": "", "module": "diq", "curl_command": "curl \"https://example.com/\"", "wsg": [ "conf-12", "o-policy" ] }],"foo2": [],"bar1": []},
         
                "anomalies": { "server error": [], "resource consumption": [] }, 
                
                "additionals": { "web technology": [], "methods": [] }, 

                "infos": { "url": "https://example.com/", "date": "thu, 08 dec 2022 06:52:04 +0000"}}}`

    var parseddata = make(map[string]interface{})
    json.unmarshal([]byte(bjson), &parseddata)
    fmt.printf("method is %s\n", parseddata["classifications"].(map[string]interface{})["material"].(map[string]interface{})["foo1"].([]interface{})[0].(map[string]interface{})["method"].(string))
}
登录后复制

如果我构建这个:

go build -o example main.go
登录后复制

它的运行方式如下:

$ ./main
method is get
登录后复制

检查值是否不存在或者是否为空列表:

data := parsedData["classifications"].(map[string]interface{})["Material"].(map[string]interface{})
val, ok := data["foo2"]
if !ok {
  panic("no key foo2 in map")
}

if count := len(val.([]interface{})); count == 0 {
  fmt.Printf("foo2 is empty\n")
} else {
  fmt.Printf("foo2 has %d items", count)
}
登录后复制

以上是在 Go Lang 中处理嵌套非结构化 JSON的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:stackoverflow.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!