首页 >后端开发 >Golang >在 Go Lang 中处理嵌套非结构化 JSON

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

WBOY
WBOY转载
2024-02-11 22:36:091040浏览

在 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删除