如何将JSON文件转换为dicts列表?

时间:2017-07-05 18:41:30

标签: python json list dictionary prtg

所以我有一个包含以下内容的JSON文件:

{"prtg-version":"17.1.29.1531","treesize":8,"probes":
    [
        {"objid":1,"name":"ASHBURN CORE"},
        {"objid":30201,"name":"GRAND RAPIDS PROBE"},
        {"objid":2,"name":"Cluster Probe"},
        {"objid":6015,"name":"AWS Oregon"},
        {"objid":2666,"name":"ASHBURN PROBE"},
        {"objid":9607,"name":"HERNDON PROBE"},
        {"objid":9218,"name":"DENVER"},
        {"objid":7259,"name":"AWS Ohio"}
    ]
}

我想在python脚本中使用这些数据,但首先我需要将此JSON转换为dicts列表,然后将该dicts列表存储到Python中的变量中。此外,如果有任何帮助,我将使用来自PRTG服务器的API调用来获取此JSON文件。我还导入了JSON模块。

基本上,如果我可以剥离外部字典,我将留下一个已经遵循Python格式的dicts列表。任何想法如何做到这一点?

1 个答案:

答案 0 :(得分:0)

这可能会有所帮助

import json

a = '{"prtg-version":"17.1.29.1531","treesize":8,"probes":[{"objid":1,"name":"ASHBURN CORE"},{"objid":30201,"name":"GRAND RAPIDS PROBE"},{"objid":2,"name":"Cluster Probe"},{"objid":6015,"name":"AWS Oregon"},{"objid":2666,"name":"ASHBURN PROBE"},{"objid":9607,"name":"HERNDON PROBE"},{"objid":9218,"name":"DENVER"},{"objid":7259,"name":"AWS Ohio"}]}'

data = json.loads(a)



for a in data:
  print(a,data[a])

我正在将响应转换为json并在for循环中迭代它