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 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算