json - 使用 Golang 解析 Json

标签 json go

我正在尝试使用 go 解析 json,但我不确定我正在构建的数据结构。输出始终是一个空对象。

数据结构:

{
  "Catalog": {
    "Name": "TypeMime",
    "Version": "1.0",
    "ObjectType": {
      "Area": 2,
      "Service": 2,
      "Version": 1,
      "Number": 117
    },
    "Items": [
      {
        "ItemNamespace": "application",
        "Name": "binhex-bin",
        "Oid": null,
        "Uid": 2201,
        "DocMandatoryProperties": [],
        "DocOptionalProperties": [
          "Subsystem",
          "Label",
          "Description"
        ],
        "DocVersionProperties": [],
        "DefaultAction": null,
        "Extensions": [
          ".bin"
        ],
        "Confidentiality": [
          "confidentiality/Public"
        ],
        "Converter": [],
        "Editor": [
          "gedit"
        ],
        "DiffEditor": [
          "kompare"
        ],
        "Icon": "",
        "IdentificationPattern": [
          "^\\S+.bin$"
        ]
      },
      {other items}
}
type ObjectType struct {
    Area int `json:"Area"`
    Service int `json:"Service"`
    Version int `json:"Version"`
    Number int `json:"Number"`
}
type Catalog struct {
    Name string `json:"Name"`
    Version string `json:"Version"`
    ObjectType ObjectType `json:"ObjectType"`
    Items []Item `json:"Items"`
}
type Item struct {
    ItemNamespace string `json:"ItemNamespaceItems"`
    Name string `json:"Name"`
    Oid int `json:"Oid"`
    Uid int `json:"Uid"`
    DocMandatoryProperties []string `json:"DocMandatoryProperties"`
    DocOptionalProperties []string `json:"DocOptionalProperties"`
    DocVersionProperties []string `json:"DocVersionProperties"`
    DefaultAction string `json:"DefaultAction"`
    Extensions []string `json:"Extensions"`
    Confidentiality []string `json:"Confidentiality"`
    Converter []string `json:"Converter"`
    Editor []string `json:"Editor"`
    DiffEditor []string `json:"DiffEditor"`
    Icon string `json:"Icon"`
    IdentificationPattern []string `json:"IdentificationPattern"`
}

    var catalog Catalog

    // Open our jsonFile
    jsonFile, err := os.Open("path.json")
    // if we os.Open returns an error then handle it
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println("Successfully Opened json")
    // defer the closing of our jsonFile so that we can parse it later on
    defer jsonFile.Close()
    byteValue, err := ioutil.ReadAll(jsonFile)
    if err != nil {
        fmt.Println("error input file :",err)
    }
    err = json.Unmarshal(byteValue, &catalog)
    if err != nil {
        fmt.Println("error input file :",err)
    }
    fmt.Println("catalog from json : ",catalog)

我得到这个输出:{ {0 0 0 0} []}

所以我在想,也许我构建的结构不正确或者 Go 无法处理“空”值。 JSON 文件已正确打开并转换为字节,我没有收到任何错误,只是不是我所期望的,即我的 Catalog 变量中的 json 解析。

有什么建议吗?

最佳答案

err = json.Unmarshal(byteValue, &catalog)

您告诉 go 您将要解析目录,但随后执行其他操作。您实际做的是给它一个(未命名的)对象,该对象具有一个具有相应值的“目录”键。 Go 在此对象中找不到任何目录键,这就是为什么您得到所有​​空字段的原因。

关于json - 使用 Golang 解析 Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56287776/

相关文章:

arrays - 使用 MarkLogic Patch Builder 在 JSON 数组的末尾插入一个新的数组项

go - 使用 golang 进行 NTP 检测,有效负载为空

go - 如何打印查询产生的多个字段?

go - 当资源超过周围函数的范围时,如何推迟资源清理?

javascript - 如何将 JSON 对象从 Java/Prime Faces 传递到 GOJS javascript 文件

json - 如何使用 bash 或 jq 在 linux 中解析 Json 文件以设置结果

javascript - JSON.parse — "unexpected token"构造的 JSON 字符串

excel - 将 Excel 十进制日期时间转换为 time.Time

go - 创建迭代 JSON 目录树 - Golang

json - 使用 jsonpath 选择多个属性