查找JSON文件的嵌套结构的值

查找JSON文件的嵌套结构的值,json,go,Json,Go,我已经成功创建了解析JSON文件所需的结构,如下所示: 我正在寻找网站访问的“协议和站点域”的值,如下所示 “协议”:“ipv4”, “站点\域”:[ "8.8.9.9", "1.1.1.1", “67.31.88.31:443” 它被深埋在结构中,但我不知道如何在通过脚本读取JSON文件后提取值。有人知道我如何实现这一点吗?我想 fmt.Println(+report.SiteVisited.Protocol.Site_domain) 但是我没有运气 { "upload&quo

我已经成功创建了解析JSON文件所需的结构,如下所示: 我正在寻找网站访问的“协议和站点域”的值,如下所示

“协议”:“ipv4”, “站点\域”:[ "8.8.9.9", "1.1.1.1", “67.31.88.31:443”

它被深埋在结构中,但我不知道如何在通过脚本读取JSON文件后提取值。有人知道我如何实现这一点吗?我想

fmt.Println(+report.SiteVisited.Protocol.Site_domain)
但是我没有运气

{
  "upload": 14234234,
  "unit_num": 154353,
  "processed": 1598558692,
  "super_report": [
    {
      "info": {
        "file": {
          "file_type": "Binary",
          "file_name": "url_list_new.zip",
          "file_path": "/home/user5",
          "size": 6654,
          "hashes": [
            {
              "name": "md5",
              "value": "fsfdsfwerfsdfsf4566f"
            },
            {
              "name": "sha1",
              "value": "8989424232gfsdfsfsdfsd"
            },
            {
              "name": "sha256",
              "value": "727206bf5c786a82ddf0bc146afff395ed2ec9sdf"
            }
          ]
        },
        "identification": {
          "success": true,
          "name": "ZIP",
          "version": "Gen5",
          "author": "EnigmaTesters"
        }
      },
      "qualification": {
        "final": false,
        "qualification": 0,
        "factor": 0,
        "scan_results": [
          {
            "type": "internal",
            "qualification": 0,
            "factor": 0,
            "name": "Azure Repo Sensor 6",
            "version": "2.7.9.3"
          }
        ]
      },
      "behavior": [
        {
          "priority": 3,
          "section": 14,
          "description": "Crashes"
        }
      ]
    },
    {
      "info": {
        "file": {
          "file_type": "PE",
          "file_name": "house.txt",
          "file_path": "/home/user5/",
          "size": 8862,
          "properties": {
            "attributes": 0,
            "modified_time": 1593481746,
            "access_time": 0,
            "creation_time": 0
          },
          "entropy": 7.8,
          "hashes": [
            {
              "name": "md5",
              "value": "sdfsdfssdfsdfsfsfsdfsdvxcvxvcv"
            },
            {
              "name": "sha1",
              "value": "xxxfsdfwr234234213sfsfsdfsfsd"
            },
            {
              "name": "sha256",
              "value": "xzfrwerwerwerfdsa890876543234234ssdfxcvsdfsdf"
            }
          ]
        }
      },
      "qualification": {
        "final": false,
        "qualification": 0,
        "factor": 0,
        "scan_results": [
          {
            "type": "internal",
            "qualification": 0,
            "factor": 0,
            "name": "Azure Sentinel",
            "version": "2.7.9.3"
          }
        ]
      },
      "behavior": [
        {
          "priority": 5,
          "protocol": 12,
          "description": "Uses /tmp folder"
        },
        {
          "priority": 5,
          "protocol": 0,
          "description": "5 dirs"
        },
        {
          "priority": 5,
          "protocol": 0,
          "description": "Creates dir"
        },
        {
          "priority": 5,
          "protocol": 10,
          "description": "updates struct"
        },
        {
          "priority": 5,
          "protocol": 12,
          "description": "Locates home"
        },
        {
          "priority": 4,
          "protocol": 22,
          "description": "Created a text file."
        },
        {
          "priority": 3,
          "protocol": 0,
          "description": "Too many stars in text file "
        }
      ],
      "sites_visted": [
        {
          "protocol": "https",
          "site_domain": [
            "bonn.sh",
            "conn.sh",
            "t00ls.ru",
            "cdr.eu"
          ]
        },
        {
          "protocol": "https",
          "site_domain": [
            "http://bin.com/jRersdf1u",
            "htto://bin.com/pxcsdfssYZ"
          ]
        },
        {
          "protocol": "ipv4",
          "site_domain": [
            "8.8.9.9",
            "1.1.1.1",
            "67.31.88.31:443"
          ]
        }
      ]
    }
  ]
}
我的围棋脚本如下:

package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "os"
)

type MainReport struct {
    Upload      int `json:"upload"`
    UnitNum     int `json:"unit_num"`
    Processed   int `json:"processed"`
    SuperReport []struct {
        Info struct {
            File struct {
                FileType    string `json:"file_type"`
                FileName    string `json:"file_name"`
                FilePath    string `json:"file_path"`
                Size        int    `json:"size"`
                Hashes      []struct {
                    Name  string `json:"name"`
                    Value string `json:"value"`
                } `json:"hashes"`
            } `json:"file"`
            Identification struct {
                Success bool   `json:"success"`
                Name    string `json:"name"`
                Version string `json:"version"`
                Author  string `json:"author"`
            } `json:"identification"`
        } `json:"info,omitempty"`
        Qualification struct {
            Final         bool `json:"final"`
            Qualification int  `json:"qualification"`
            Factor        int  `json:"factor"`
            ScanResults   []struct {
                Type          string `json:"type"`
                Qualification int    `json:"qualification"`
                Factor        int    `json:"factor"`
                Name          string `json:"name"`
                Version       string `json:"version"`
            } `json:"scan_results"`
        } `json:"qualification"`
        Behavior []struct {
            Priority    int    `json:"priority"`
            Section     int    `json:"section"`
            Description string `json:"description"`
        } `json:"behavior"`
        Info2 struct {
            File struct {
                FileType    string `json:"file_type"`
                FileSubtype string `json:"file_subtype"`
                FileName    string `json:"file_name"`
                FilePath    string `json:"file_path"`
                Size        int    `json:"size"`
                Properties  struct {
                    Attributes   int `json:"attributes"`
                    ModifiedTime int `json:"modified_time"`
                    AccessTime   int `json:"access_time"`
                    CreationTime int `json:"creation_time"`
                } `json:"properties"`
                Entropy float64 `json:"entropy"`
                Hashes  []struct {
                    Name  string `json:"name"`
                    Value string `json:"value"`
                } `json:"hashes"`
            } `json:"file"`
        } `json:"info,omitempty"`
        SitesVisted []struct {
            Protocol   string   `json:"protocol"`
            SiteDomain []string `json:"site_domain"`
        } `json:"sites_visted,omitempty"`
    } `json:"super_report"`
}

// main fuction is below
func main() {

    jsonFile, err := os.Open("file.json")
    if err != nil {
        fmt.Println(err)
    }

    fmt.Println("Successfully Opened json file")
    defer jsonFile.Close()

    fmt.Println("Successfully Opened file.json")

    byteEmpValue, _ := ioutil.ReadAll(jsonFile)

    var report MainReport

    json.Unmarshal(byteEmpValue, &report)

    fmt.Println(+report.Processed)

    fmt.Printf("%+v\n", report)
}

这只是一个例子,你能给我一个你想要的结果的例子吗

主程序包
进口(
“编码/json”
“fmt”
)
类型myStruct struct{
Upload int`json:“Upload”`
UnitNum int`json:“unit_num”`
已处理int`json:“已处理”`
超级报表[]结构{
信息结构{
文件结构{
FileType字符串`json:“文件类型”`
文件名字符串`json:“文件名”`
FilePath字符串`json:“文件路径”`
Size int`json:“大小”`
哈希[]结构{
名称字符串`json:“名称”`
值字符串`json:“值”`
}`json:“散列”`
}`json:“文件”`
标识结构{
Success bool`json:“Success”`
名称字符串`json:“名称”`
版本字符串`json:“版本”`
Author字符串`json:“Author”`
}`json:“识别”`
}`json:“信息,省略空”`
资格结构{
Final bool`json:“Final”`
Qualification int`json:“Qualification”`
Factor int`json:“Factor”`
扫描结果[]结构{
类型字符串`json:“类型”`
Qualification int`json:“Qualification”`
Factor int`json:“Factor”`
名称字符串`json:“名称”`
版本字符串`json:“版本”`
}`json:“扫描结果”`
}`json:“资格”`
行为[]结构{
Priority int`json:“优先级”`
Section int`json:“Section”`
Description字符串`json:“Description”`
}`json:“行为”`
Info2结构{
文件结构{
FileType字符串`json:“文件类型”`
文件名字符串`json:“文件名”`
FilePath字符串`json:“文件路径”`
Size int`json:“大小”`
属性结构{
Attributes int`json:“Attributes”`
ModifiedTime int`json:“修改的时间”`
AccessTime int`json:“访问时间”`
CreationTime int`json:“创建时间”`
}`json:“属性”`
熵浮动64`json:“熵”`
哈希[]结构{
名称字符串`json:“名称”`
值字符串`json:“值”`
}`json:“散列”`
}`json:“文件”`
}`json:'info2,省略空'`
SitesListed[]结构{
协议字符串`json:“协议”`
SiteDomain[]字符串`json:“站点\域”`
}`json:“站点已访问,省略为空”`
}`json:“超级报告”`
}
func main(){
a:=`{“上传”:14234234,“单位数”:154353,“处理”:1598558692,“超级报告”:[{“信息”:“{”文件“:{”文件类型“:“二进制”,“文件名”:“url\u列表\u new.zip”,“文件路径”:/home/user5”,“大小”:6654,“散列”:[{“名称”:“md5”,“值”:“FSFDFWFRF4566F”},{“名称”:“sha1”,“值”:“89894232GFSDFSDFSD”},“名称”:“sha256”,“值”:“727206bf5c786a82ddf0bc146afff395ed2ec9sdf”},“识别”:{“成功”:真,“名称”:“ZIP”,“版本”:“Gen5”,“作者”:“谜测试员”},“资格”:{“最终”:假,“资格”:0,“因子”:0,“扫描结果”:[{“类型”:“内部”,“资格”:0,“因子”:0,“名称”:“Azure回购传感器6”,“版本”:“2.7.9.3”},“行为”:[{“优先级”:3”,第节“:14,“描述”:“崩溃”}],{“信息”:{“文件”:{“文件类型”:“PE”,“文件名”:“house.txt”,“文件路径”:“/home/user5/”,“大小”:8862,“属性”:{“属性”:0,“修改时间”:1593481746,“访问时间”:0,“创建时间”:0},“熵”:7.8,“哈希”:[{“名称”:“md5”,“值”:“SDFSDFSSDFSDDVXCVSVCV”},{“名称”:“sha1”,“值”:”“XXXFSDFWR2342342213SFSDFSD”},{“名称”:“sha256”,“值”:“XZFREWERVERFDSA890876543234234SSDFxCVSDFSDF”},},{“最终”:假,“限定”:0,“因子”:0,“扫描结果”:[{“类型”:“内部”,“限定”:0,“因子”:0,“名称”:“Azure哨兵”,“版本”:“2.7.9.3”},{“行为”:[{“优先级”:5,“协议”:12,“描述”:”使用/tmp文件夹“},{“优先级”:5,“协议”:0,“描述”:“5目录”},{“优先级”:5,“协议”:0,“描述”:“创建目录”},{“优先级”:5,“协议”:10,“描述”:“更新结构”},{“优先级”:5,“协议”:12,“描述”:“定位主页”},{“优先级”:4,“协议”:22,“描述”:“创建了一个文本文件”},{“优先级”:3,“协议”:0,“描述”“:”文本文件中的星星太多“}],”站点访问“:[{”协议“:”https“,”站点域“:[”bonn.sh“,”conn.sh“,”t00ls.ru“,”cdr.eu“]},{”协议“:”https“,”站点域“:[”http://bin.com/jRersdf1u","htto://bin.com/pxcsdfssYZ“]},{“协议”:“ipv4”,“站点_域”:[“8.8.9.9”,“1.1.1.1”,“67.31.88.31:443”]}`
var myStruct myStruct
变量地址[]字符串
如果err:=json.Unmarshal([]字节(a),&myStruct);err!=nil{
恐慌(错误)
}
对于_,报告:=范围myStruct.SuperReport{
对于uk:=范围报告。站点已列出{
fmt.Printf(“%+v\n”,k)
地址=附加(地址,k.SiteDomain…)
}
}
fmt.Println(地址)
}
输出:

{Protocol:https SiteDomain:[bonn.sh conn.sh t00ls.ru cdr.eu]}
{Protocol:https SiteDomain:[http://bin.com/jRersdf1u htto://bin.com/pxcsdfssYZ]}
{Protocol:ipv4 SiteDomain:[8.8.9.9 1.1.1.1 67.31.88.31:443]}
[bonn.sh conn.sh t00ls.ru cdr.eu http://bin.com/jRersdf1u htto://bin.com/pxcsdfssYZ 8.8.9.9 1.1.1.1 67.31.88.31:443]

SuperReport、SitesVisited和SiteDomain都是结构的切片,要访问切片中的元素,您需要将其索引到切片中,或者您可以在该切片上循环/范围