简体   繁体   English

如何在tcl中进行JSON编组?

[英]How to do JSON marshalling in tcl?

I am trying to create a JSON for one of our requirement, I want to do JSON marshelling in my Tcl script, Is there any way to do the same in Tcl. 我正在尝试为我们的要求之一创建JSON,我想在我的Tcl脚本中进行JSON编组,是否有任何方法可以在Tcl中执行相同的操作。 Any solution with example will be more useful. 任何带有示例的解决方案将更加有用。

The json::write package in Tcllib is recommended, but it's pretty primitive. 推荐使用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]
]]

That produces this output when I try it: 尝试时会产生以下输出:

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

You can also use the (evil, type-aware) technique described on Rosetta Code ; 您还可以使用Rosetta Code上描述的(邪恶的,可识别类型的)技术; that builds on top of the json::write package (now that I've rewritten it…) and lets you do: 它建立在json :: write包的顶部(现在我已经对其进行了重写……),并可以执行以下操作:

puts [tcl2json [dict create "abc" $abc "def" [list 123 $pqr $stu]]]

to generate the same output. 产生相同的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM