首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >迭代geoJSON

迭代geoJSON
EN

Stack Overflow用户
提问于 2016-04-20 22:11:28
回答 1查看 2.5K关注 0票数 0

我的geoJSON看起来是这样

代码语言:javascript
运行
复制
{
"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

代码语言:javascript
运行
复制
for features in geoJsonPoints["features"]:
    for interesting in features["properties"]["value1"]:
        print interesting
        print "!"

我得到了

一个 好了! B 好了! C 好了!

为什么?!好像我的循环不给我一本字典?!

如果我这么做

代码语言:javascript
运行
复制
for features in geoJsonPoints["features"]:
    for interesting in features["properties"]:
        print type(intereseting)
        print interesting

我得到了

类型'unicode‘ value1 类型'unicode‘ value2 ..。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-20 22:16:50

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

代码语言:javascript
运行
复制
for property_name, property_value in features["properties"].items():
    print(property_name, property_value)

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

代码语言:javascript
运行
复制
for property_name in features["properties"]:
    print(property_name, features["properties"][property_name])

请参阅有关字典和循环技术的更多信息:

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36756144

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档