dongshaidu2456 2018-01-22 19:22
浏览 62
已采纳

如何在Golang中访问嵌套的Json键值

Team, new to Programming. I have data available after unmarshaling the Json as shown below, which has nested Key values. flat key values I am able to access, how do I access nested key values. Here is the byte slice data shown below after unmarshaling —>

tables:[map[name:basic__snatpool_members] map[name:net__snatpool_members] map[name:optimizations__hosts] map[columnNames:[name] name:pool__hosts rows:[map[row:[ry.hj.com]]]] traffic_group:/Common/traffic-group-1

Flat key values I am able to access by using the following code

p.TrafficGroup = m[“traffic_group”].(string)

here is the complete function

func dataToIapp(name string, d *schema.ResourceData) bigip.Iapp {
        var p bigip.Iapp

        var obj interface{}

        jsonblob := []byte(d.Get("jsonfile").(string))
        err := json.Unmarshal(jsonblob, &obj)
        if err != nil {
                fmt.Println("error", err)
        }
        m := obj.(map[string]interface{}) // Important: to access property
        p.Name = m[“name”].(string)
        p.Partition = m[“partition”].(string)

        p.InheritedDevicegroup = m[“inherited_devicegroup”].(string)

}
  • 写回答

3条回答 默认 最新

  • doubipiao1611 2018-01-22 20:17
    关注

    Note: This may not work with your JSON structure. I inferred what it would be based on your question but without the actual structure, I cannot guarantee this to work without modification.

    If you want to access them in a map, you need to assert that the interface pulled from the first map is actually a map. So you would need to do this:

    tmp := m["tables"]
    tables, ok := tmp.(map[string]string)
    if !ok {
        //error handling here
    }
    
    r.Name = tables["name"].(string)
    

    But instead of accessing the unmarshaled JSON as a map[string]interface{}, why don't you create structs that match your JSON output?

    type JSONRoot struct {
        Name string `json:"name"`
        Partition string `json:"partition"`
        InheritedDevicegroup string `json:"inherited_devicegroup"`
        Tables map[string]string `json:"tables"` //Ideally, this would be a map of structs
    }
    

    Then in your code:

    func dataToIapp(name string, d *schema.ResourceData) bigip.Iapp {
        var p bigip.Iapp
    
        var obj &JSONRoot{}
    
        jsonblob := []byte(d.Get("jsonfile").(string))
        err := json.Unmarshal(jsonblob, &obj)
        if err != nil {
                fmt.Println("error", err)
        }
    
        p.Name = obj.Name
        p.Partition = obj.Partition
    
        p.InheritedDevicegroup = obj.InheritedDevicegroup
    
        p.Name = obj.Tables["name"]
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥25 关于##爬虫##的问题,如何解决?:
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误