[英]Extract key value from JSON using python NameError
I am creating a python script to extract values, Name from the JSON Key Details from the JSON result.我正在创建一个 python 脚本来提取值,名称来自 JSON 密钥详细信息来自 JSON 结果。 Python error mentioned KeyError 'details[name]'.
Python 错误提到 KeyError 'details[name]'。 The JSON example is found below.
JSON 示例如下所示。 The JSON is incomplete.
JSON 不完整。 JSON has other data which I am not going to put it here as it is confidential.
JSON 还有其他数据,我不打算在这里放,因为它是机密的。
details: {'id': 5555, 'name': 'Timothy', 'Gender': 'Male'}
My Python script is shown below我的 Python 脚本如下所示
print(json_data['details[name]'])
Error Message错误信息
print(json_data['details[name]'])
KeyError: 'details[name]'
I want to print the result我想打印结果
Timothy
What am I missing?我错过了什么?
do it one key at a time rather than trying to fit both the keys into one set of quotes as you originally had.一次做一个键,而不是像原来那样尝试将两个键都放入一组引号中。
print(json_data['details']['name'])
Assuming json_data
is the name you've chosen for the entirety of the JSON object, you need provide a proper index for json_data
in your print statement.假设
json_data
是您为整个 JSON object 选择的名称,您需要在打印语句中为json_data
提供正确的索引。 Your current index is a string
because you've captured your brackets inside of the apostrophes.您当前的索引是一个
string
,因为您已经在撇号内捕获了括号。 The way you're indexing the JSON object is also incorrect.您索引 JSON object 的方式也不正确。
JSON objects are essentially multidimensional dictionaries. JSON 对象本质上是多维字典。 The way you print the value of a value of a key is like this:
print(dict["key"]["value"])
.打印键值的值的方式是这样的:
print(dict["key"]["value"])
。
Assuming the details
key is also a string, the correct way to print the value of name
from the key "details"
is: print(json_data["details"]["name"]
假设
details
键也是一个字符串,从键"details"
打印name
的值的正确方法是: print(json_data["details"]["name"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.