将元素添加到 TCL 字典中的数组

Add element to an array in a dictionary in TCL

我很难将元素添加到字典中的数组(列表?)中,但无法解决这个问题。

我正在从 json 对象创建字典,大致如下所示:

{"channel1":{"badwords":["test", "demo"]}, "channel2":{"badwords":["remove"]}}

这个JSON转换成字典,我可以访问坏词作为

[dict get $channels "channel1" "badwords"]

我需要通过向它添加一个新值来更新字典,基本上使它成为:

{"channel1":{"badwords":["test", "demo", "new"]}, "channel2":{"badwords":["remove"]}}

我发现字典有点棘手。依赖文档:http://tcl.tk/man/tcl8.6/TclCmd/dict.htm

set json {{"channel1":{"badwords":["test", "demo"]}, "channel2":{"badwords":["remove"]}}}
package require json  # from tcllib

set channels [json::json2dict $json]
# ==> channel1 {badwords {test demo}} channel2 {badwords remove}

dict update channels channel1 subdict1 {dict lappend subdict1 badwords "new"}
# or, "dict with" that sets local variables you can manipulate
dict with channels channel1 {lappend badwords "new"}

set channels
# ==> channel1 {badwords {test demo new}} channel2 {badwords remove}