1 回答
TA贡献1836条经验 获得超5个赞
如果您在单独的文件中有 JSON,则需要读取该文件才能导入with open('./data.json') as f. 此外,if 'W' in a似乎不正确,因为您的 JSON blob 将其作为 string 而不是 object "W/M":"M"。所以它看起来像:
data.json
{
"member_acct":{
"email":"example@gmail.com",
"password":"examplepassword"
},
"product_info":{
"product_name":"Nike Airforce 1s",
"W/M":"M",
"size":"9.5"
}
}
main.py
import json
with open('./data.json') as f:
data = json.load(f)
a = data["product_info"]["W/M"]
# the value of a is a string and not a dictionary
if a == 'W':
print("This is a woman's shoe")
# the value of this is a string and not a dictionary
if float(data["product_info"]["size"]) == 5:
print("size 5")
else:
print("This is not a size 5")
else:
print ("this is a men's shoe")
if float(data["product_info"]["size"]) == 3.5:
print("size 3.5")
else:
print("This is not a size 3.5")
注意:已编辑,不错,JSON 大小值是浮点数,而不是整数。调整为将值转换为浮动
添加回答
举报