dongtui8593 2018-01-23 16:31
浏览 60
已采纳

如何使用golang打印json文档中“ org”下当前字段中存储的所有元素?

The updated json is as follows:

    {
     "phone":[
                {"home":"58878767"},
                {"mobile":"32453543"}
              ],
     "org": [
    {
        "current": {
            "org_dept": "Hr",
            "org_eptime": "1516354574432",
            "org_name": "Uejsjak",
            "org_title": "Hakosklaks"
        }
    },
    {
        "current": {
            "org_dept": "Accounts",
            "org_eptime": "1516354561184",
            "org_name": "Abcd",
            "org_title": "Hakahkshsjs"
        },
    {
        "past": {
            "org_dept": "Backend",
            "org_eptime": "15163545",
            "org_name": "Ab",
            "org_title": "Hakah"
        }
    }
    ]
    }

I am using the following code to print on the key and values:

    personMap := make(map[string][]map[string]string)

    json.Unmarshal([]byte(ii), &personMap)


    for key, value := range personMap {
    fmt.Println("index : ", key, " value : ", value){

     }

The output I am getting is:

    index: org value: [map["current":""],map["current":""]

How I can print every value of fields under field "current"?????

Now I am doing this:

    personMap := make(map[string][]struct{Current map[string]string})
    json.Unmarshal([]byte(ii), &personMap)
    for key, value := range personMap {
    fmt.Println("index : ", key, " value : ", value)

    }

The output I am getting is:

    index :  org  value :  [{map[org_dept:Hr org_eptime:1516354574432 org_name:Uejsjak org_title:Hakosklaks]} {map[org_dept:Accounts org_eptime:1516354561184 org_name:Abcd org_title:Hakahkshsjs]}]
    index :  phone  value :  [{map[]} {map[]}]
  • 写回答

1条回答 默认 最新

  • dongrui6787 2018-01-23 19:43
    关注

    The contents of phone and org are completely different data structures, and you won't be able to cleanly deserialize both into a homogenous format like you've got in the example. The best option is to at least partially deserialize into a struct:

    type data struct {
        Phone []map[string]string
        Org []map[string]map[string]string
    }
    

    This will at least deserialize all of the data, but it's still a bit messy; a slice of maps of maps is not a great data structure to work with. It's not clear from the question, but if any of the fields are fixed, you might want to codify those in types as well, for example:

    type data struct {
        Phone []map[string]string
        Org []struct{
            Current map[string]string
        }
    }
    

    You can then deserialize to this type and use it much more easily:

    var person data
    json.Unmarshal([]byte(ii), &person)
    fmt.Printf("%v", person.Phone)
    fmt.Printf("%v", person.Org[0].Current)
    

    Working playground example here: https://play.golang.org/p/5W-7RzPimZj

    Note that I had to correct an error in the JSON, it is invalid due to a missing comma before "org".

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

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了