doujiekeyan0622 2018-09-23 13:16
浏览 595
已采纳

遍历JSON键和值,并同时替换golang中的指定匹配值

Is there any way to loop all over keys and values of json and thereby confirming and replacing a specific value by matched path or matched compared key or value and simultaneously creating a new interface of out of the json after being confirmed with the key new value in Golang.

This an example i saw that loops through all values https://play.golang.org/p/xtiT2iGocBg but i have no idea of replacing values by matched path or value

  • 写回答

2条回答 默认 最新

  • dsoxcj7276 2018-09-26 22:18
    关注

    Finally! I completed the full specification of what i were looking for!!

    https://play.golang.org/p/eN4-FjaQS97

       package main
    
    import (
        "encoding/json"
        "fmt"
    )
    
    func main() {
        b := []byte(`
            {
            "iw":{"Ie":{"Itye":{"e":"eIe"}}},
    "InnerJSON2":"NoneValue",
        "outterJSON":{
            "innerJSON1":{
                "value1":10,
                "value2":22
                ,
                "InnerInnerArray": [ "test1" , "test2"],
                "InnerInnerJSONArray": [ {"fld1" : "val1"} , {"fld2" : "val2"} ]
                },
                "InnerJSON2":"NoneValue"
            }
        }
        `)
    
        f := map[string]interface{}{}
        if err := json.Unmarshal(b, &f); err != nil {
            panic(err)
        }
    
        verifyJSON(f)
        data, _ := json.MarshalIndent(f, "", "  ")
        fmt.Println(string(data))
    }
    
    func verifyJSON(bv interface{}) {
    
        var dumpJSON func(v interface{}, kn string)
    
        dumpJSON = func(v interface{}, kn string) {
            iterMap := func(x map[string]interface{}, root string) {
    
                var knf string
                if root == "root" {
                    knf = "%v/%v"
                } else {
                    knf = "%v/%v"
                }
                for k, v := range x {
    
                    switch vv := v.(type) {
    
                    case map[string]interface{}:
    
                        fmt.Printf("%s => (map[string]interface{}) ...
    ", fmt.Sprintf(knf, root, k))
    
                    case []interface{}:
                        fmt.Printf("%s => ([]interface{}) ...
    ", fmt.Sprintf(knf, root, k))
                    default:
                        fmt.Printf("%s => %v
    ", fmt.Sprintf(knf, root, k), vv)
                        x[k] = "rgk"
                    }
                    dumpJSON(v, fmt.Sprintf(knf, root, k))
                }
            }
    
            iterSlice := func(x []interface{}, root string) {
                var knf string
                if root == "root" {
                    knf = "%v/%v"
    
                } else {
                    knf = "%v/%v"
                }
                for k, v := range x {
    
                    switch vv := v.(type) {
    
                    case map[string]interface{}:
    
                        fmt.Printf("%s => (map[string]interface{}) ...
    ", fmt.Sprintf(knf, root, k))
    
                    case []interface{}:
                        fmt.Printf("%s => ([]interface{}) ...
    ", fmt.Sprintf(knf, root, k))
                    default:
    
                        fmt.Printf("%s => %v
    ", fmt.Sprintf(knf, root, k), vv)
    
                        x[k] = "rg"
                    }
    
                    dumpJSON(v, fmt.Sprintf(knf, root, k))
                }
            }
    
            switch vv := v.(type) {
            case map[string]interface{}:
                iterMap(vv, kn)
            case []interface{}:
                iterSlice(vv, kn)
            default:
    
            }
        }
        dumpJSON(bv, "root")
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器