【问题标题】:How to quote JSON in expect?如何在期望中引用 JSON?
【发布时间】:2017-12-02 11:32:28
【问题描述】:

我已经编写了下面的 Expect 脚本,它可以工作。

我希望只有一根发送线和一根接收线。

(如果我在大括号内添加\n,它会在需要发送 0x10 时按字面意思发送。)

#!/usr/bin/expect

# N.B. The outer set of curly brackets are quotes in TCL/expect.
# The JSON received by the server only has a *single* set of outer curly
# brackets.
# The \n is sent on a separate line as the curly quote sends a literal \n,
# rather than a 0x10.  (Double quotes *do* send a 0x10 for \n, but then I
# would have to escape every double quote inside the JSON.)

set timeout 30
spawn nc localhost 2222
send {{"method":"GET", "path":["ping"]}}
send "\n"
expect {{"code": 204}}
expect "\n"

如何在 Expect 中以更易读的方式发送和期待单行 JSON?

如何避免 sending 和 expecting 的 \ns 在自己的线路上?

【问题讨论】:

  • 通过 Expect 控制的通道交换 JSON 似乎是一件非常奇怪的事情。 (您是否考虑过生成 JSON 而不是使用文字?)
  • @DonalFellows 我正在 (ab) 使用 expect 来测试 JSON 服务器...Expect 只是客户端的模拟。
  • 即便如此,我仍然会在变量中真正构建对象字符串,然后执行send "$json\n" 或类似的操作将其移出。

标签: json tcl expect double-quotes curly-braces


【解决方案1】:

怎么样

append json {{"method":"GET", "path":["ping"]}} \n
send $json

如果你有最新(8.6.2 或更高版本)的 Tcl,你应该可以使用

send [string cat {{"method":"GET", "path":["ping"]}} \n]

【讨论】:

    猜你喜欢
    • 2017-06-17
    • 2018-07-09
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    • 2016-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多