Python 在 json 中找不到密钥

Python cannot find key in a json

我在文本文件中有以下 json:

{
"defaultSymbol" : {
          "type" : "CIMSymbolReference",
          "symbol" : {
            "type" : "CIMPointSymbol",
            "symbolLayers" : [
              {
                "type" : "CIMVectorMarker",
                "enable" : true,
                "anchorPointUnits" : "Relative",
                "dominantSizeAxis3D" : "Z",
                "size" : 4,
                "billboardMode3D" : "FaceNearPlane",
                "frame" : {
                  "xmin" : -2,
                  "ymin" : -2,
                  "xmax" : 2,
                  "ymax" : 2
                },
                "markerGraphics" : [
                  {
                    "type" : "CIMMarkerGraphic",
                    "geometry" : {
                      "curveRings" : [
                        [
                          [
                            1.2246467991473532e-16,
                            2
                          ],
                          {
                            "a" : [
                              [
                                1.2246467991473532e-16,
                                2
                              ],
                              [
                                0,
                                0
                              ],
                              0,
                              1
                            ]
                          }
                        ]
                      ]
                    },
                    "symbol" : {
                      "type" : "CIMPolygonSymbol",
                      "symbolLayers" : [
                        {
                          "type" : "CIMSolidStroke",
                          "enable" : true,
                          "capStyle" : "Round",
                          "joinStyle" : "Round",
                          "lineStyle3D" : "Strip",
                          "miterLimit" : 10,
                          "width" : 1,
                          "color" : {
                            "type" : "CIMRGBColor",
                            "values" : [
                              0,
                              0,
                              0,
                              100
                            ]
                          }
                        },
                        {
                          "type" : "CIMSolidFill",
                          "enable" : true,
                          "color" : {
                            "type" : "CIMRGBColor",
                            "values" : [
                              0,
                              3,
                              173,
                              100
                            ]
                          }
                        }
                      ]
                    }
                  }
                ],
                "respectFrame" : true
              }
            ],
            "haloSize" : 1,
            "scaleX" : 1,
            "angleAlignment" : "Map"
          }
        }
}

我还有以下python代码递归打印每个键值对:

def recursive_items(dictionary):
    for key, value in dictionary.items():
        if type(value) is dict:
            print("value is dict: " +str(type(value)))
            yield (key, value)
            yield from recursive_items(value)
        else:
            print("value is not dict: " +str(type(value)))
            yield (key, value)

f = open('TestJsonMultipleSymbols.txt')

# returns JSON object as
# a dictionary
data = json.load(f)

for key, value in recursive_items(data):
    print("Key: " + str(key), "Value: " + str(value))
    print("Next")
print("done")

这会打印所有键并将 'symbol' 显示为我要查找并打印其值的键。 但是,当我通过修改代码搜索密钥 'symbol' 时,它 returns 什么也没有。我在这里错过了什么?这是修改后的代码:

def recursive_items(dictionary):
    for key, value in dictionary.items():
        search_key = 'symbol'
        if search_key in dictionary: 
            if type(value) is dict:
                print("value is dict: " +str(type(value)))
                yield (key, value)
                yield from recursive_items(value)
            else:
                print("value is not dict: " +str(type(value)))
                yield (key, value)

好吧,检查 if search_key in dictionary 不是 递归的。在您的第一次调用中,它将检查 symbol 是否是最外层字典中的键。顶级.

现在,symbol 不是该字典中的键,因此该函数不执行任何操作就直接退出。