我正在寻找一种读取多个 JSON 数组的特定值的方法。
我能够对它们进行计数,现在想从它们中获取每个特定值,但我无法找到将 [0]、[1]、[2] 等设置为变量的方法。
LineCount = len(obj['data']['monitors']);
print ("Active Lines:", LineCount)
LineNb = "obj['data']['monitors'][" + LineCount + "]['lines'][0]['name']"
LineName = json.dumps(LineNb)
print (LineCount, "Line", LineName)
while LineCount > 1:
LineCount -= 1
LineNb = "obj['data']['monitors'][" + LineCount + "]['lines'][0]['name']"
print (LineCount, "Line", LineName)
虽然不推荐,但快速解决方法是
eval
字符串:
LineNb = "obj['data']['monitors'][" + LineCount + "]['lines'][0]['name']"
LineNb = eval(LineNb)
...