强烈推荐:一个快速处理json的第三方包
该第三方包采用go语言开发:https://github.com/tidwall/sjson
先来看看官方的案例:
package main import "github.com/tidwall/sjson" const json = `{"name":{"first":"Janet","last":"Prichard"},"age":47}` func main() { value, _ := sjson.Set(json, "name.last", "Anderson") println(value) }
输出:
{"name":{"first":"Janet","last":"Anderson"},"age":47}
可以看到,直接把last对应的值搞成了 Anderson
如果是多维呢?按照官方的示例,自己做了一个简单的demo:
const json = `{ "demo": [ { "name": "name", "title": "title", "info": {"key":"val"}, "url": "http://goo.gl" }, { "name": "name_2", "title": "title_2", "info": {"key_2":"val_2"}, "url": "https://google.com" } ] }` value, _ := sjson.Set(json, "demo.1.url", "http://baidu.com") println(value)
输出
{ "demo": [{ "name": "name", "title": "title", "info": { "key": "val" }, "url": "http://goo.gl" }, { "name": "name_2", "title": "title_2", "info": { "key_2": "val_2" }, "url": "http://baidu.com" } ] }
可以看出demo下的第二个url参数已经发生改变了!
如果需要值全部改变可以用
sjson.Set(json, "demo.#.url", "xxx")
声明:版权所有,违者必究 | 如未注明,均为原创 | 本网站采用 BY-NC-SA 协议进行授权
转载:转载请注明原文链接,违者必究 - :https://wolfcode.net/info/234/