繁体   English   中英

如何使用python从json文件中选择值

[英]how to select values from json file using python

我有一个从json文件读取的python函数,必须将这些数据写入表小部件中。

JSON文件包含:(处理列表的字典,其中项目列表是字典)

  • 字典
  • 清单
  • 字典

我试图进行循环并从json文件中读取,但未成功。

json档案

 "default":"dbLocal",
            "DB":[
                {
                 "name":"dbLocal",
                 "source":"dbtest",
                 "type":"sqlite3",
                 "comment":"Initially created DB"
                }
            ]
        }

function.py

def writeIntoTable(self):
    with open(os.path.join(self.homeDir,self.subDir,self.refFile)) as refJsonHandler:
        jsonData = json.load(refJsonHandler)
        #print(jsonData)
        for distro in jsonData:
            print(distro[''])<== here i tried to put the key of dictionary like "name"            

系统显示此错误:

writeIntoTable print(distro ['source'])中文件“ app.py”的第79行,TypeError:字符串索引必须为整数

迭代字典时,您正在遍历键。

for distro in jsonData:
    print(distro['']) # "distro" is the key "default"

要从键中获取值:

for distro in jsonData:
    print(jsonData[distro]) # This is the value of dict "jsonData" at key "distro"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM