dongyun7897 2019-05-24 07:29
浏览 63

使用Golang解析Json

I am trying to parse json using go, but I am not sure about the data structure I am building. The output is always an empty object.

data structure :

{
  "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)

I am getting this output : { {0 0 0 0} []}

So I am thinking, maybe the structures that I have built are not correct or Go cannot handle "null" values. The JSON file is correctly opened and converted to bytes, I am not getting any errors, just not what I was expecting i.e. the json parse in my Catalog variable.

Any suggestions ?

  • 写回答

1条回答 默认 最新

  • drnmslpz42661 2019-05-24 07:39
    关注
    err = json.Unmarshal(byteValue, &catalog)
    

    You tell go that you're about to parse a Catalog, but then do something else. What you actually do is give it an (unnamed) object that has a "Catalog" key with corresponding value. Go can't find any of catalog keys in this object and that's why you get all empty fields.

    评论

报告相同问题?

悬赏问题

  • ¥15 python变量和列表之间的相互影响
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)