我在一个文件中有这个JSON:
{"groupcolor":[
{"user":"group01", "color":"blue"},
{"user":"group02", "color":"yellow"},
{"user":"group03", "color":"green"}
]}
我想使用Python(3)验证“user”的内容是否与“color”匹配。我试过:
import json
with open('groupcolor.json') as f:
for line in f:
if f.user == group01 and f.color = blue:
print("ok!")
else:
print ("not ok")
但它显然不是正确的语法。我发现的大部分信息都集中在解析或添加信息上,但是我没有发现任何关于检查两个元素之间关系的信息。是用Python实现的方法吗?你知道吗
这里有一个解决方案:
你肯定有正确的想法:正如你所指出的,只是语法错误。你知道吗
如注释所示,您需要使用
json.load()
(但不能使用json.loads()
,因为json.loads()
表示字符串,而不是文件)。这将在json文件中作为字典进行绑定。你知道吗相关问题 更多 >
编程相关推荐