1

我试图从 websocket 获取输出到 eggdrop tcl 脚本.. websocket 部分正在工作,我得到这样的输出:

{
    "action": "insert",
    "row": {
        "id": 7814727,
        "name": "Doom_Squad-Countdown_To_Doomsday_II-WEB-2016-ESG",
        "team": "ESG",
        "cat": "MP3",
        "genre": "",
        "url": "",
        "size": 0,
        "files": 0,
        "preAt": 1493884429,
        "nuke": null
    }
}

我如何循环或映射项目?它对我来说看起来像 json,所以我尝试了以下操作:这适用于同一站点的另一个输出,只有该输出包含 []。

上面的输出没有,那么它真的是json吗?

set asadict [::json::json2dict $body]

lmap item [dict get $asadict row] {
    dict filter $item key name team cat genre size files preAt nuke
}

foreach item [dict get $asadict row]  {
    dict with item {

       set humanReadableDate [clock format $preAt]

    putquick "PRIVMSG $chan :\003\[\0037PRE\003\]\003\[\0033$cat\003\]\003 $name \003\[\00312PRETIME\003\]\003 \002$humanReadableDate\002 \[PRE-INFO\] \[Group: $team\] \[Size: $size MB\] \[Files: $files\] \[Genre: $genre\] \[Nuked: $nuke\]"

    }
}
4

1 回答 1

1

好吧,因为只有一个 的元素row,所以你不需要循环它:

set asadict [::json::json2dict $body]

set item [dict get $asadict row]
dict filter $item key name team cat genre size files preAt nuke

dict with item {
   set humanReadableDate [clock format $preAt]
   putquick "PRIVMSG $chan :\003\[\0037PRE\003\]\003\[\0033$cat\003\]\003 $name \003\[\00312PRETIME\003\]\003 \002$humanReadableDate\002 \[PRE-INFO\] \[Group: $team\] \[Size: $size MB\] \[Files: $files\] \[Genre: $genre\] \[Nuked: $nuke\]"
}

当你试图循环它时,你实际上是一次循环遍历键和值。

于 2020-01-07T21:35:09.193 回答