从 json 字符串的自定义文本中获取日期
Getting the date from customText from a json string
我正在尝试遍历这个 json 字符串
"layoutOptions": {
"titleText": "Route Number",
"authorText": "West LA Yard",
"copyrightText": "",
"customTextElements": [{
"Date": "9/11/2018, 7:37:35 AM"
}
],
"scaleBarOptions": {
"metricUnit": "esriKilometers",
"metricLabel": "km",
"nonMetricUnit": "esriMiles",
"nonMetricLabel": "mi"
},
"legendOptions": {
"operationalLayers": [{
"id": "ParcelRouteEditingTest_1458"
}, {
"id": "ParcelRouteEditingTest_1259"
}
]
}
}
我保留运行这个错误list indices must be integers, not str
layoutOpsDict = layoutData["layoutOptions"]
dateList = [dateEle["customTextElements"]["Date"] for dateEle in layoutOpsDict]
错误:
追溯(最近一次通话最后一次):
File "<pyshell#44>", line 1, in <module>
dateList = [dateEle["customTextElements"]["Date"] for dateEle in layoutOpsDict]
TypeError: string indices must be integers, not str
除了不断设置更多变量以跟踪之外,在 customTextElements 中获取日期的最佳方法是什么?
您正在遍历每个键,而不仅仅是 "customTextElements"
,并且并非所有键都有以 "Date"
作为键的字典列表。
因为您只想查看映射到 "customTextElements"
的值,您只能循环遍历:
dateList = [dateEle["Date"] for dateEle in layoutOpsDict["customTextElements"]]
我正在尝试遍历这个 json 字符串
"layoutOptions": {
"titleText": "Route Number",
"authorText": "West LA Yard",
"copyrightText": "",
"customTextElements": [{
"Date": "9/11/2018, 7:37:35 AM"
}
],
"scaleBarOptions": {
"metricUnit": "esriKilometers",
"metricLabel": "km",
"nonMetricUnit": "esriMiles",
"nonMetricLabel": "mi"
},
"legendOptions": {
"operationalLayers": [{
"id": "ParcelRouteEditingTest_1458"
}, {
"id": "ParcelRouteEditingTest_1259"
}
]
}
}
我保留运行这个错误list indices must be integers, not str
layoutOpsDict = layoutData["layoutOptions"]
dateList = [dateEle["customTextElements"]["Date"] for dateEle in layoutOpsDict]
错误: 追溯(最近一次通话最后一次):
File "<pyshell#44>", line 1, in <module>
dateList = [dateEle["customTextElements"]["Date"] for dateEle in layoutOpsDict]
TypeError: string indices must be integers, not str
除了不断设置更多变量以跟踪之外,在 customTextElements 中获取日期的最佳方法是什么?
您正在遍历每个键,而不仅仅是 "customTextElements"
,并且并非所有键都有以 "Date"
作为键的字典列表。
因为您只想查看映射到 "customTextElements"
的值,您只能循环遍历:
dateList = [dateEle["Date"] for dateEle in layoutOpsDict["customTextElements"]]