【问题标题】:Handling keys inside a key from .json file处理来自 .json 文件的密钥中的密钥
【发布时间】:2020-09-22 08:32:57
【问题描述】:

我正在尝试从 JSON 文件中打印特定密钥,但没有成功。

.json 文件是:

{
    "testing": [
        {
            "_name": "teste",
            "_profile_telphone": "212331233",
            "_age": "21"
        }
    ]
}

我正在使用的功能是:

def load(self):
    filename = self.profile_entry.get()
    if filename == "":
        msg = "Insert a profile name"
        messagebox.showinfo("Profile name is empty", msg)
        self.profile_entry.focus()
    else:
        with open('profile_'+filename+'.json', 'r') as outfile:
            data = json.load(outfile)
            print(data[filename])
            outfile.close()

print(data[filename])我可以打印整个

{
            "_name": "teste",
            "_profile_telphone": "212331233",
            "_age": "21"
        }

但是我怎样才能只打印姓名或年龄?

【问题讨论】:

标签: python json python-3.x


【解决方案1】:

data 是 JSON 列表 - 你有一个 json 数组,为了解析它,你可以使用 for 语句来完成

for element in data[filename]:
    print(element[‘_name’])
    print(element[‘_profile_telphone’])
    print(element[‘_age’])

【讨论】:

  • 谢谢@Alin Stelian 这就是我想要的。这个数组谢谢!!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-10
  • 2020-11-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多