【发布时间】:2020-02-14 17:46:22
【问题描述】:
使用以下json:
{
"operationalLayers": [
{
"id": "GRSM_STORM_TOTAL_PRECIP_8083",
"layerType": "ArcGISFeatureLayer",
"url": "https://services1.arcgis.com/fBc8EJBxQRMcHlei/arcgis/rest/services/GRSM_STORM_TOTAL_PRECIP/FeatureServer/0",
"visibility": true,
"opacity": 0.8,
"title": "GRSM_STORM_TOTAL_PRECIP",
"itemId": "20370bddfa7948bb98cf5f9e143b44b2",
"layerDefinition": {
"drawingInfo": {
"renderer": {
"visualVariables": [
{
"type": "colorInfo",
"field": "INCHES",
"valueExpression": null,
"stops": [
{
"value": 0.753,
"color": [
255,
252,
212,
255
],
"label": "< 0.7"
},
{
"value": 2.91475,
"color": [
238,
190,
139,
255
],
"label": null
},
{
"value": 5.0765,
"color": [
221,
128,
66,
255
],
"label": "5.07"
},
{
"value": 7.238250000000001,
"color": [
162,
64,
33,
255
],
"label": null
},
{
"value": 9.4,
"color": [
102,
0,
0,
255
],
"label": "> 9.3"
}
]
},
{
"type": "sizeInfo",
"target": "outline",
"expression": "view.scale",
"valueExpression": "$view.scale",
"stops": [
{
"size": 1.5,
"value": 13459
},
{
"size": 0.75,
"value": 42058
},
{
"size": 0.375,
"value": 168232
},
{
"size": 0,
"value": 336463
}
]
}
],
"authoringInfo": {
"visualVariables": [
{
"type": "colorInfo",
"minSliderValue": 0.753,
"maxSliderValue": 9.4,
"theme": "high-to-low"
}
]
},
"type": "classBreaks",
"field": "INCHES",
"minValue": -9007199254740991,
"classBreakInfos": [
{
"symbol": {
"color": [
170,
170,
170,
255
],
"outline": {
"color": null,
"width": 0.75,
"type": "esriSLS",
"style": "esriSLSNull"
},
"type": "esriSFS",
"style": "esriSFSSolid"
},
"classMaxValue": 9007199254740991
}
]
}
}
},
"popupInfo": {
"title": "GRSM_STORM_TOTAL_PRECIP: {Id}",
"fieldInfos": [
{
"fieldName": "OBJECTID",
"label": "OBJECTID",
"isEditable": false,
"tooltip": "",
"visible": false,
"stringFieldOption": "textbox"
},
{
"fieldName": "Id",
"label": "Id",
"isEditable": true,
"tooltip": "",
"visible": true,
"stringFieldOption": "textbox",
"format": {
"places": 0,
"digitSeparator": true
}
},
{
"fieldName": "gridcode",
"label": "gridcode",
"isEditable": true,
"tooltip": "",
"visible": true,
"stringFieldOption": "textbox",
"format": {
"places": 0,
"digitSeparator": true
}
},
{
"fieldName": "INCHES",
"label": "INCHES",
"isEditable": true,
"tooltip": "",
"visible": true,
"stringFieldOption": "textbox",
"format": {
"places": 2,
"digitSeparator": true
}
},
{
"fieldName": "TIMESTAMP",
"label": "TIMESTAMP",
"isEditable": true,
"tooltip": "",
"visible": true,
"stringFieldOption": "textbox",
"format": {
"dateFormat": "shortDateShortTime"
}
},
{
"fieldName": "Shape__Area",
"label": "Shape__Area",
"isEditable": false,
"tooltip": "",
"visible": false,
"stringFieldOption": "textbox",
"format": {
"places": 2,
"digitSeparator": true
}
},
{
"fieldName": "Shape__Length",
"label": "Shape__Length",
"isEditable": false,
"tooltip": "",
"visible": false,
"stringFieldOption": "textbox",
"format": {
"places": 2,
"digitSeparator": true
}
}
],
"description": null,
"showAttachments": true,
"mediaInfos": []
}
}
],
"baseMap": {
"baseMapLayers": [
{
"id": "World_Hillshade_3689",
"layerType": "ArcGISTiledMapServiceLayer",
"url": "https://services.arcgisonline.com/arcgis/rest/services/Elevation/World_Hillshade/MapServer",
"visibility": true,
"opacity": 1,
"title": "World Hillshade"
},
{
"id": "VectorTile_6451",
"type": "VectorTileLayer",
"layerType": "VectorTileLayer",
"title": "World Topographic Map",
"styleUrl": "https://www.arcgis.com/sharing/rest/content/items/7dc6cea0b1764a1f9af2e679f642f0f5/resources/styles/root.json",
"itemId": "7dc6cea0b1764a1f9af2e679f642f0f5",
"visibility": true,
"opacity": 1
}
],
"title": "Topographic"
},
"spatialReference": {
"wkid": 102100,
"latestWkid": 3857
},
"authoringApp": "WebMapViewer",
"authoringAppVersion": "7.4",
"version": "2.16"
}
我需要替换一些值
"visualVariables": [
{
"type": "colorInfo",
"field": "INCHES",
"valueExpression": null,
"stops": [
{
"value": 0.753,
"color": [
255,
252,
212,
255
],
"label": "< 0.7"
},
{
"value": 2.91475,
"color": [
238,
190,
139,
255
],
"label": null
},
{
"value": 5.0765,
"color": [
221,
128,
66,
255
],
"label": "5.07"
},
{
"value": 7.238250000000001,
"color": [
162,
64,
33,
255
],
"label": null
},
{
"value": 9.4,
"color": [
102,
0,
0,
255
],
"label": "> 9.3"
}
]
}
例如输出是
"visualVariables": [
{
"type": "colorInfo",
"field": "INCHES",
"valueExpression": null,
"stops": [
{
"value": 0.5,
"color": [
255,
252,
212,
255
],
"label": "< 0.5"
},
{
"value": 5,
"color": [
238,
190,
139,
255
],
"label": 5
},
{
"value": 15,
"color": [
221,
128,
66,
255
],
"label": "15"
},
{
"value": 10,
"color": [
162,
64,
33,
255
],
"label": 10
},
{
"value": 20,
"color": [
102,
0,
0,
255
],
"label": "> 20"
}
]
}
到目前为止,我所做的只是例如一一读取值
import json
from pprint import pprint
with open('C:/Temp/NEXRAD_PYTHON/new 1.json') as f:
data = json.load(f)
data["operationalLayers"][0]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][2]["value"]
5.0765
data["operationalLayers"][0]["layerDefinition"]["drawingInfo"]["renderer"]["visualVariables"][0]["stops"][2]["label"]
'5.07'
但我想做的是有一个单独的(列表?字典),例如
['.5', '5', '10', '15', '20']
在此工作流程之外更新(每 10 分钟,由于另一个程序的结果,数字将更改一次),将替换“值”和“标签”。
我看过 Change json.dumps dictionary values in python, Replace multiple keys and values of JSON file in Python, Python - change JSON values and pretty print,和 https://www.geeksforgeeks.org/python-ways-to-change-keys-in-dictionary/
这要么不符合我的情况,要么我遗漏了一些明显的东西。
【问题讨论】:
标签: python json python-3.x