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

遍历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条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)