如何格式化json以输出

时间:2019-02-17 11:47:43

标签: json tcl eggdrop

我有这个json链接,https://predb.ovh/api/v1/?q=@name%20IfIca.Icssssy

返回的JSON是

{
    "status": "success",
    "message": "",
    "data": {
        "rowCount": 0,
        "rows": [],
        "offset": 0,
        "reqCount": 20,
        "total": 0,
        "time": 0.003080273
    }
}

输出状态和消息正常工作。 当rowCount也出现在输出中时,我得到一个错误:

  

Tcl错误:无法读取“ rowCount”:没有这样的变量

bind pub "-|-" !search pub:test
proc pub:test { nick host handle channel arg } {

    set name [lindex $arg 0]
    set tok [http::geturl "https://predb.ovh/api/v1/?q=@name%20$name"]

    set aadata [json::json2dict [http::data $tok]]
    http::cleanup $tok  
    dict with aadata {
        putnow "PRIVMSG $channel :status $status"
        putnow "PRIVMSG $channel :rowCount $rowCount"
    }

}

1 个答案:

答案 0 :(得分:1)

问题在于密钥rowCount不在aadata字典的直接下方,而是位于子字典中。

如果您想使用dict with,则必须这样做

dict with aadata {
    putnow "PRIVMSG $channel :status $status"
    dict with data {
        putnow "PRIVMSG $channel :rowCount $rowCount"
    }
}

或更简单:

putnow "PRIVMSG $channel :status [dict get $aadata status]"
putnow "PRIVMSG $channel :rowCount [dict get $aadata data rowCount]"