Python 插入到 JSON

huangapple go评论68阅读模式
英文:

Python Insert into json

问题

  1. 要将 "Daniel.Michaelson@outlook.com:1234567" 插入 JSON 中,你可以这样做:
# 先将 JSON 字符串解析为 Python 字典
import json

json_data = '''
{
    "One": "catherine.hamilton@outlook.com:1234567",
    "Two": ["catherine.hamilton@outlook.com:1234567", "catherine.hamilton@outlook.com:1234567"]
}
'''

data = json.loads(json_data)

# 然后插入新数据
data["One"] = "Daniel.Michaelson@outlook.com:1234567"
data["Two"] = [""]
  1. 要在 "Two" 中插入 Matt.Demon@outlook.com 的另一行,你可以这样做:
# 添加 Matt.Demon@outlook.com 的另一行到 "Two"
data["Two"].append("Matt.Demon@outlook.com:1234567")

希望这些代码对你有所帮助!

英文:

so I need to make a little json database with Python and JSON and insert new data into it.

Here's what I am trying to do:

{
	"One": "catherine.hamilton@outlook.com:1234567",
	"Two": ["catherine.hamilton@outlook.com:1234567", "catherine.hamilton@outlook.com:1234567"]
}

So I want to insert multiple instances of like this:

{
	"One": "catherine.hamilton@outlook.com:1234567",
	"Two": ["catherine.hamilton@outlook.com:1234567", "catherine.hamilton@outlook.com:1234567"],
	"One": "John.Jones@outlook.com:1234567",
	"Two": ["John.Jones@outlook.com:1234567", "John.Jones@outlook.com:1234567"],
	"One": "Matt.Demon@outlook.com:1234567",
	"Two": ["Matt.Demon@outlook.com:1234567", "Matt.Demon@outlook.com1234567"],
	"One": "Adam.Baker@outlook.com:1234567",
	"Two": ["Matt.Demon@outlook.com:1234567", "Matt.Demon@outlook.com:1234567"],

}

(I know this is invalid json syntax, i'm not sure what the correct would be).

  1. Let's say I want to insert "Daniel.Michaelson@outlook.com:1234567" into the json, so it would add

    "One": "Daniel.Michaelson@outlook.com:1234567",
    "Two": [""],

How would I do it?

  1. Let's say I want to insert another row into Two, for Matt.Demon@outlook.com, how would I do this in Python?

Thank you!

答案1

得分: 0

"如您所说,这不是有效的 JSON,所以您的问题不太清晰。JSON 由键和值构建,但键需要是单射的。这意味着每两个键应该彼此不同。

回到您的问题。在 Python 中,字典用于存储类似 JSON 的数据。因此,实际上您需要将所有数据存储在字典中,然后将其转换为 JSON。例如:

import json

data = {"One": "catherine.hamilton@outlook.com:1234567",
        "Two": ["catherine.hamilton@outlook.com:1234567", "catherine.hamilton@outlook.com:1234567"]
       }

json_s = json.dumps(data)

如果您已经有一个 JSON 字符串并希望将其转换为字典,请使用 loads

data2 = json.loads(json_s)

"

英文:

As you said this isn't a valid json so your question isn't very clear. Jsons are built from keys and values, however keys need to be injective. This means that every two keys should be different from each other.

Going back to your question. In python dictionary is used to store a json like data. So you actually need to store all your data in a dictionary and than convert it to json. for example:

import json

data = {"One": "catherine.hamilton@outlook.com:1234567",
    "Two": ["catherine.hamilton@outlook.com:1234567", "catherine.hamilton@outlook.com:1234567"]
        }

json_s = json.dumps(data)

if you already have a json string and want to convert it to dictionary use loads:

data2 = json.loads(json_s)

答案2

得分: -1

This is the basic idea. You might consider having an "import_data" function that reads from your file and returns the data structure, and an "export_data" function that writes it back out.

import json
jjj = """
[
    {
        "One": "catherine.hamilton@outlook.com:1234567",
        "Two": ["catherine.hamilton@outlook.com:1234567", "catherine.hamilton@outlook.com:1234567"]
    }
]"""

data = json.loads(jjj)

data.append({
    "One": "John.Jones@outlook.com:1234567",
    "Two": ["John.Jones@outlook.com:1234567", "John.Jones@outlook.com:1234567"]
})
data.append({
    "One": "Matt.Demon@outlook.com:1234567",
    "Two": ["Matt.Demon@outlook.com:1234567", "Matt.Demon@outlook.com1234567"]
})
data append({
    "One": "Adam.Baker@outlook.com:1234567",
    "Two": ["Matt.Demon@outlook.com:1234567", "Matt.Demon@outlook.com:1234567"]
})

print(json.dumps(data, indent=4))

Output:

[
    {
        "One": "catherine.hamilton@outlook.com:1234567",
        "Two": [
            "catherine.hamilton@outlook.com:1234567",
            "catherine.hamilton@outlook.com:1234567"
        ]
    },
    {
        "One": "John.Jones@outlook.com:1234567",
        "Two": [
            "John.Jones@outlook.com:1234567",
            "John.Jones@outlook.com:1234567"
        ]
    },
    {
        "One": "Matt.Demon@outlook.com:1234567",
        "Two": [
            "Matt.Demon@outlook.com:1234567",
            "Matt.Demon@outlook.com1234567"
        ]
    },
    {
        "One": "Adam.Baker@outlook.com:1234567",
        "Two": [
            "Matt.Demon@outlook.com:1234567",
            "Matt.Demon@outlook.com:1234567"
        ]
    }
]
英文:

This is the basic idea. You might consider having an "import_data" function that reads from your file and returns the data structure, and an "export_data" function that writes it back out.

import json
jjj = """\
[
    {
        "One": "catherine.hamilton@outlook.com:1234567",
        "Two": ["catherine.hamilton@outlook.com:1234567", "catherine.hamilton@outlook.com:1234567"]
    }
]"""

data = json.loads( jjj )

data.append({
    "One": "John.Jones@outlook.com:1234567",
    "Two": ["John.Jones@outlook.com:1234567", "John.Jones@outlook.com:1234567"]
})
data.append({
    "One": "Matt.Demon@outlook.com:1234567",
    "Two": ["Matt.Demon@outlook.com:1234567", "Matt.Demon@outlook.com1234567"]
})
data.append({
    "One": "Adam.Baker@outlook.com:1234567",
    "Two": ["Matt.Demon@outlook.com:1234567", "Matt.Demon@outlook.com:1234567"]
})

print(json.dumps(data, indent=4))

Output:

[
    {
        "One": "catherine.hamilton@outlook.com:1234567",
        "Two": [
            "catherine.hamilton@outlook.com:1234567",
            "catherine.hamilton@outlook.com:1234567"
        ]
    },
    {
        "One": "John.Jones@outlook.com:1234567",
        "Two": [
            "John.Jones@outlook.com:1234567",
            "John.Jones@outlook.com:1234567"
        ]
    },
    {
        "One": "Matt.Demon@outlook.com:1234567",
        "Two": [
            "Matt.Demon@outlook.com:1234567",
            "Matt.Demon@outlook.com1234567"
        ]
    },
    {
        "One": "Adam.Baker@outlook.com:1234567",
        "Two": [
            "Matt.Demon@outlook.com:1234567",
            "Matt.Demon@outlook.com:1234567"
        ]
    }
]

huangapple
  • 本文由 发表于 2023年3月21日 01:40:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75793558-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定