data = {'virtualInterfaces': [{"interfaceName": "abc", "interfaceState": "online"}, {"interfaceName": "axc", "interfaceState": "online"}, {"interfaceName": "xpto", "interfaceState": "offline"}]}
foo = "['virtualInterfaces'][2]['interfaceState']"
我需要使用var“foo”从data
获取一个特定的值
例如
data[foo] #should return "offline"
我试着用evalvalue = eval(data + foo)
Foo可以有任意数量的嵌套list/dict
1条答案
按热度按时间yqkkidmi1#
foo
包含文字索引字符串,而data
不是字符串,而是字典对象。所以你必须把data
放在引号中才能使用eval
: