将 JSON 数组设置为 json.dumps 的变量

问题描述 投票:0回答:1

我正在寻找一种读取多个 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)
python arrays json variables
1个回答
0
投票

虽然不推荐,但快速解决方法是

eval
字符串:

LineNb = "obj['data']['monitors'][" + LineCount + "]['lines'][0]['name']"
LineNb = eval(LineNb)

...
© www.soinside.com 2019 - 2024. All rights reserved.