如何格式化 json 输出

how to format json to output

我有这个jsonlink,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 error : can't read "rowCount": no such variable

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"
    }

}

问题是关键 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]"