Python中的JSON数据解析和映射--根据value查询key
import json
# 原始 JSON 数据
json_data = '''
[
{"id": 1, "name": "MOVE"},
{"id": 2, "name": "LEFT_DOWN"},
{"id": 3, "name": "LEFT_UP"},
{"id": 4, "name": "RIGHT_DOWN"},
{"id": 5, "name": "RIGHT_UP"},
{"id": 6, "name": "SCROLL_DOWN"},
{"id": 7, "name": "SCROLL_UP"},
{"id": 8, "name": "SCROLL_PUSH"},
{"id": 9, "name": "SCROLL_PULL"}
]
'''
# 将JSON数据解析为Python对象
data = json.loads(json_data)
# 选择特定id和name的组合
result_dict = {entry['name']: entry['id'] for entry in data}
print(result_dict.get('MOVE'))