如何在tcl中进行JSON编组?

pan*_*ish 1 json tcl marshalling unmarshalling

我正在尝试为我们的一个要求创建一个JSON,我想在我的Tcl脚本中进行JSON marshelling,有没有办法在Tcl中做同样的事情.任何带有示例的解决方案都会更有用.

Don*_*ows 6

建议使用Tcllib中的json :: write包,但它非常原始.

package require json::write

set abc {This is an example with "quotes" in it}
set pqr "yet another string"
set stu "and another"
puts [json::write object abc [json::write string $abc] def [
    json::write array \
       123 \
       [json::write string $pqr] \
       [json::write string $stu]
]]
Run Code Online (Sandbox Code Playgroud)

当我尝试时产生这个输出:

{
    "abc" : "This is an example with \"quotes\" in it",
    "def" : [123,"yet another string","and another"]
}

您还可以使用Rosetta Code中描述的(邪恶的,类型感知的)技术; 它建立在json :: write包之上(现在我已经重写了它...)并允许你这样做:

puts [tcl2json [dict create "abc" $abc "def" [list 123 $pqr $stu]]]
Run Code Online (Sandbox Code Playgroud)

生成相同的输出.