Python 仅在遍历json数据时获取字符串
JSON: 我试图用python从“csevents”(“2807”、“2810”、“3601”)中获取成员。问题是我在编码时不知道“9”(“1695056”、“1695309”)中的ID 所以我试着遍历“9”,然后遍历“csevents”,但是如果遍历“9”,我只得到一个字符串,所以我不能再遍历“csevents” Python:Python 仅在遍历json数据时获取字符串,python,json,api,Python,Json,Api,JSON: 我试图用python从“csevents”(“2807”、“2810”、“3601”)中获取成员。问题是我在编码时不知道“9”(“1695056”、“1695309”)中的ID 所以我试着遍历“9”,然后遍历“csevents”,但是如果遍历“9”,我只得到一个字符串,所以我不能再遍历“csevents” Python: { "status": "success", "data": { "9": { "1695056": {
{
"status": "success",
"data": {
"9": {
"1695056": {
"id": "1695056",
[...]
},
"csevents": {
"2807": {
"id": "2807",
"startdate": "2019-01-24 18:45:00",
"service_texts": [],
"eventTemplate": "1"
},
"2810": {
"id": "2810",
"startdate": "2019-01-31 18:45:00",
"service_texts": [],
"eventTemplate": "1"
}
}
},
"1695309": {
"id": "1695309",
[...]
},
"csevents": {
"3601": {
"id": "3601",
"startdate": "2019-05-17 18:45:00",
"service_texts": [],
"eventTemplate": "1"
}
那不行,有人知道我怎么解决吗
谢谢必须清理JSON字符串以使其正常工作,但查看您的解决方案似乎是直接从
目录进行迭代,您应该使用的是.items()
或.values()
:
您应该逐字地发布json内容(相关部分),以及脚本的输出(以及print(whatever)
,在内部循环之前)。那么rand_dict=whatever['csevents'][id]
呢?
for whatever in json_object['data']['9']:
for id in whatever['csevents']:
print(id)
for key, value in json_object['data']['9'].items():
# We can use .keys() here since we only need the IDs from csevents
csevent_keys = list(value['csevents'].keys())
print(csevent_keys)
# Output
['2807', '2810']
['3601']