dongqiao1964 2019-05-03 14:39
浏览 66
已采纳

如何将所有解码的数据写入JSON文件?

I try to parse an XML data to a JSON file, but when I begin writing marshaled data to a JSON, it just rewrites data in the JSON file and, as a result, I have the file with last XML element. How to write the whole data into the JSON file?

Snippet of code that parses XML and marshal data to JSON

    decoder := xml.NewDecoder(file)
    resultData := map[string]map[string]string{}
    for {
        t, _ := decoder.Token()
        if t == nil {
            break
        }

        switch et := t.(type) {
        case xml.StartElement:
            if et.Name.Local == "profile" {
                var object XMLProfile
                decoder.DecodeElement(&object, &et)

                resultData = map[string]map[string]string{
                    object.ProfileName: {},
                }

                for _, val := range object.Fields {
                    resultData[object.ProfileName][val.Name] = val.Value
                }
            }
        }
    }

    if out, err := json.MarshalIndent(resultData, "", "\t"); err != nil {
        panic(err)
    } else {
        _ = ioutil.WriteFile("test.json", out, 0644)
    }

Expect JSON:

{
  "Profile 1": {
      "role": "user"
  },

  "Profile 2": {
      "role": "user"
  },

  "Profile 3": {
      "role": "admin"
  }
}

Actual JSON:

{
  "Profile 3": {
    "role": "admin"
  }
}
  • 写回答

1条回答 默认 最新

  • dongtanliefang8765 2019-05-03 16:06
    关注

    Seems like you are recreating the resultData after each iteration in the nodes named "profile". As that happens, only the last one will reach the code where you write the JSON.

    Try this:

    decoder := xml.NewDecoder(file)
    resultData := map[string]map[string]string{}
    for {
        t, _ := decoder.Token()
        if t == nil {
            break
        }
    
        switch et := t.(type) {
        case xml.StartElement:
            if et.Name.Local == "profile" {
                var object XMLProfile
                decoder.DecodeElement(&object, &et)
    
                resultData[object.ProfileName] = map[string]string{}
    
                for _, val := range object.Fields {
                    resultData[object.ProfileName][val.Name] = val.Value
                }
            }
        }
    }
    
    if out, err := json.MarshalIndent(resultData, "", "\t"); err != nil {
        panic(err)
    } else {
        _ = ioutil.WriteFile("test.json", out, 0644)
    }
    

    I would also check if no duplicate ProfileName happens to appear in the XML, as it would override the previous entry.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 上传图片时提交的存储类型
  • ¥15 Ubuntu开机显示器只显示kernel,是没操作系统(相关搜索:显卡驱动)
  • ¥15 VB.NET如何绘制倾斜的椭圆
  • ¥15 在rhel8中安装qemu-kvm时遇到“cannot initialize crypto:unable to initialize gcrypt“报错”
  • ¥15 arbotix没有/cmd_vel话题
  • ¥15 paddle库安装时报错提示需要安装common、dual等库,安装了上面的库以后还是显示报错未安装,要怎么办呀?
  • ¥20 找能定制Python脚本的
  • ¥15 odoo17的分包重新供应路线如何设置?可从销售订单中实时直接触发采购订单或相关单据
  • ¥15 用C语言怎么判断字符串的输入是否符合设定?
  • ¥15 通信专业本科生论文选这两个哪个方向好研究呀