Tcl:是否有更有效的方法来检索从 JSON 转换而来的字典中的嵌套值?

Tcl: Is there a more efficient way to retrieve a nested value in a dictionary that was converted from JSON?

我想知道是否有更有效的方法来检索与输入键配对的 'id' 值 7777777。我正在使用 Tcllib json 包。

package require json

set json {
{
    "resourceType": "Bundle",
    "id": "11111111-22222-3cc3-4444-d5d555dd5555",
    "type": "searchset",
    "total": 1,
    "link": [
        {
            "relation": "self",
            "url": "https://selfurl.com/a54"
        }
    ],
    "entry": [
        {
            "fullUrl": "https://test.sandboxtest.com/e2/1a11aaa1-22bb-3333-4444dd-e5e5ee55ee55/Patient/lastname=test",
            "resource": {
                "resourceType": "Patient",
                "id": "7777777",
                "meta": {
                    "versionId": "5",
                    "lastUpdated": "2020-10-08T18:26:31.000Z"
                }
            }
        }
    ]
}
}

set jsDict [json::json2dict $json]
set entryVal [dict get $jsDict entry]

if {[regexp {Patient id ([^ ]+)} $entryVal match idVal]} {
    puts $idVal
}

您应该对 JSON 数组使用列表操作。所以像这样:

puts [dict get [lindex [dict get $jsDict entry] 0] resource id]

或者,您可以直接使用 rl_json 从 $json 中提取数据:

puts [rl_json::json get $json entry 0 resource id]