遍历 geoJSON

Iterating over geoJSON

我的 geoJSON 看起来像这样

{
"type": "FeatureCollection",
"crs": {
    "type": "name",
    "properties": {
        "name": "urn:ogc:def:crs:OGC:1.3:CRS84"
    }
},

"features": [{
        "type": "Feature",
        "properties": {
            "value1": "abc",
            "value2": 0,
            "value3": 0.99,
            "value4": "def",
            "value5": "882.3",
            "value6": 12,
        },
        "geometry": {
            "type": "Point",
            "coordinates": [1, 1]
        }
    }
]
}

我想访问 properties 并检查一些 values 是否有 key

for features in geoJsonPoints["features"]:
    for interesting in features["properties"]["value1"]:
        print interesting
        print "!"

我明白了

a

!

b

!

c

!

这是为什么?!好像我的循环没有 return me a dictionary?!

如果我这样做

for features in geoJsonPoints["features"]:
    for interesting in features["properties"]:
        print type(intereseting)
        print interesting

我明白了

type 'unicode'

value1

type 'unicode'

value2

...

为什么那不是字典?而且,如果它不是字典,为什么我可以像我展示的第一个循环那样访问“unicode”后面的值?!

features["properties"]["value1"] 指向 abc 字符串,您可以逐个字符地遍历该字符串。相反,您可能打算遍历 properties 字典:

for property_name, property_value in features["properties"].items():
    print(property_name, property_value)

或者,您可以遍历字典键:

for property_name in features["properties"]:
    print(property_name, features["properties"][property_name])

在此处查看有关字典和循环技术的更多信息: