我想知道如何编写一个lambda函数来查找json中带有键“ title”的所有值。这背后的原因是因为我正在尝试使用这些元素构建图例。我想使其尽可能通用。将来,我可能需要将更多“标题”元素收集到List中,以便在ArcPy的映射函数中进行操作。
这是我正在使用的json数据。
{
"mapOptions": {
"showAttribution": true,
"extent": {
"xmin": -13208269.297921617,
"ymin": 4049185.0103628845,
"xmax": -13204562.102049686,
"ymax": 4052218.6049230327,
"spatialReference": {
"wkid": 102100,
"latestWkid": 3857
}
},
"spatialReference": {
"wkid": 102100
},
"scale": 18055.954822
},
"operationalLayers": [{
"id": "defaultBasemap",
"title": "Topographic",
"opacity": 1,
"minScale": 0,
"maxScale": 0,
"url": "https://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer"
}, {
"id":
"WV_Base_Route_Webmap_022019_Comments_RELATE_ArcGIS_ONLINE_2110",
"title": "Comments",
"opacity": 1,
"minScale": 0,
"maxScale": 0,
"layerDefinition": {
"drawingInfo": {
"renderer": {
"type": "simple",
"label": "",
"description": "",
"symbol": {
"color": [153, 74, 0, 255],
"size": 4,
"angle": 0,
"xoffset": 0,
"yoffset": 0,
"type": "esriSMS",
"style": "esriSMSCircle",
"outline": {
"color": [0, 0, 0, 255],
"width": 1,
"type": "esriSLS",
"style": "esriSLSSolid"
}
}
}
}
},
"token": "aOO0npo0L2ShYruhcLkSPQjofYj8XM0WKVE_GNbtKE-WgC4nB_t0jkuWGesN1bxz9VCum0DuVwEcePax4r7Tj5PlHFQAjqbLdFx2k_YqQrN6q9mjybGG00TLm-G_4j6NVfLxxmIXpGSH5vUM5s2L_F9vIW5VP2KsIzgOlGvZSzUbauzPjd5eMaobheEM0g3BZ4FYluEGcEZ1bFL4GnRkPJgQAo4Rj64uEygJJWrlXb-QmUKiA5Ibq5fHglxzv-7I7AY61cLO0VMF_23tHybBcnSi_zsFYkhKImLWOzrmTuA.",
"url": "https://services1.arcgis.com/X1hcdGx5Fxqn4d0j/arcgis/rest/services/WV_Base_Route_Webmap_022019_Comments_RELATE_ArcGIS_ONLINE/FeatureServer/1"
}, {
"id": "WV_Base_Route_Webmap_022019_Comments_RELATE_ArcGIS_ONLINE_1115",
"title": "Route_Title",
"opacity": 1,
"minScale": 0,
"maxScale": 0,
"layerDefinition": {
"drawingInfo": {
"renderer": {
"type": "simple",
"label": "",
"description": "",
"symbol": {
"color": [156, 0, 132, 255],
"width": 1,
"type": "esriSLS",
"style": "esriSLSSolid"
}
}
},
"definitionExpression": "((UPPER(Map_Title) = 'FRI-AA-07'))"
},
"token": "aOO0npo0L2ShYruhcLkSPQjofYj8XM0WKVE_GNbtKE-WgC4nB_t0jkuWGesN1bxz9VCum0DuVwEcePax4r7Tj5PlHFQAjqbLdFx2k_YqQrN6q9mjybGG00TLm-G_4j6NVfLxxmIXpGSH5vUM5s2L_F9vIW5VP2KsIzgOlGvZSzUbauzPjd5eMaobheEM0g3BZ4FYluEGcEZ1bFL4GnRkPJgQAo4Rj64uEygJJWrlXb-QmUKiA5Ibq5fHglxzv-7I7AY61cLO0VMF_23tHybBcnSi_zsFYkhKImLWOzrmTuA.",
"url": "https://services1.arcgis.com/X1hcdGx5Fxqn4d0j/arcgis/rest/services/WV_Base_Route_Webmap_022019_Comments_RELATE_ArcGIS_ONLINE/FeatureServer/0"
}
],
"exportOptions": {
"outputSize": [670, 500],
"dpi": 96
},
"layoutOptions": {
"titleText": "ArcGIS Web Map",
"authorText": "Web AppBuilder for ArcGIS",
"copyrightText": "County of Los Angeles, Bureau of Land Management, Esri, HERE, Garmin, INCREMENT P, USGS, METI/NASA, EPA, USDA | VENOM | ",
"customTextElements": [{
"Date": "3/5/2019, 2:37:09 PM"
}
],
"scaleBarOptions": {
"metricUnit": "esriKilometers",
"metricLabel": "km",
"nonMetricUnit": "esriMiles",
"nonMetricLabel": "mi"
},
"legendOptions": {
"operationalLayers": [{
"id": "WV_Base_Route_Webmap_022019_Comments_RELATE_ArcGIS_ONLINE_2110"
}, {
"id": "WV_Base_Route_Webmap_022019_Comments_RELATE_ArcGIS_ONLINE_1115"
}
]
}
}
}
结果是['Topographic', 'Comments', 'Route_Title']
答案 0 :(得分:1)
遍历json中的所有节点,就可以得到结果。您的json是嵌套的,因此您应该递归遍历它。因为lambda
创建的函数没有命名,所以很难递归遍历嵌套的json。
def find_value(dic):
if isinstance(dic, dict):
result = []
for k, v in dic.items():
if k == 'title':
result.append(v)
else:
result.extend(find_value(v))
return result
else:
return [dic]
a = {
"dd": {
"title": 23
},
"title": {
"a": 115
}
}
print find_value(a) # output:[23, {'a': 115}]