首页 > 解决方案 > Golang SJSON 动态 JSON

问题描述

我在我的 golang 项目中使用 sjson。我想在我的项目中设置一些键值对。我有一个非结构化的动态对象。所以我无法知道路径。如下所示:

{

    "temp1": {
        "temp2": {
            "password": "123456",
            "country": "turkey",
            "temp3": {
                "password": "789654"
            }
        }
    }
}

我想将密码值编辑为“秘密”,但在我的程序中我不知道路径。有没有前缀-posfix等...我该如何处理这个问题?

标签: jsongosjson

解决方案


我在不使用 sjson 的情况下解决了它,而是使用如下递归函数:

func changePassword(myMap map[string]interface{}) {
    for key, value := range myMap {
        if key == "password" {
            myMap [key] = "******"
        }
        if _, ok := value.(map[string]interface{}); ok {
            changePassword(value.(map[string]interface{}))
        }

    }
}

推荐阅读