Json入口登录阅读器

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

我用 python 创建了一个 Json 读取器,它从名为 data.json 的文件中读取数据,但我不知道如何从名为 Amazon 的条目中读取名为 Login 的条目

{
    "Facebook": [
        {
            "Login": "John",
            "Password": "mycatisgreat"
        }
    ],
    "Twitch": [
        {
            "Login": "matelek",
            "Password": "password"
        }
    ],
    "Xkom": [
        {
            "Login": "neural",
            "Password": "neuralpass"
        }
    ],
    "Amazon": [
        {
            "Login": "john",
            "Password": "amazonisgreat777"
        }
    ]
}

这是 data.json 文件

import json


def json_read():
    with open("data.json", "r") as f:
        data = json.load(f)
        Amazon_content = data.get("Amazon")

        if Amazon_content is not None:

            Amazon_join = ", ".join(str(item) for item in Amazon_content)
            Amazon_strip = Amazon_join.replace("{", "")
            Amazon_strip_2 = Amazon_strip.replace("'", "")
            Amazon_strip_3 = Amazon_strip_2.replace("}", "")
            print(f"Amazon:\n    {Amazon_strip_3}")

        with open("json_output.txt", "w") as file:
            file.write(
                f"Amazon:\n    {Amazon_strip_3}"
            )


json_read()

谢谢。

我尝试了

Amazon_content = data.get("Amazon", "Login")
但它不起作用我期待某种类型的数据但我没有得到任何

python json reader jsonreader
1个回答
0
投票
  import json

def json_read():
    with open("data.json", "r") as f:
        data = json.load(f)
        Amazon_content = data.get("Amazon")

        if Amazon_content is not None:
            # Assuming there is only one entry in the "Amazon" section
            amazon_entry = Amazon_content[0]
            amazon_login = amazon_entry.get("Login")

            if amazon_login is not None:
                print(f"Amazon Login: {amazon_login}")

                with open("json_output.txt", "w") as file:
                    file.write(f"Amazon Login: {amazon_login}")

json_read()

尝试使用此代码!

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