如何在python中附加到现有的json数组

时间:2019-04-14 02:37:32

标签: python json

我想使用python这样附加到现有的json数组:

{
      "username": "user",
      "password": "password"
    }

from this to 
{
      "username": "user",
      "password": "password",
      "team name": "testTeam",
      "firstplayer": "firstPlayer",
      "secondplayer": "secondPlayer",
      "thirdplayer": "thirdPlayer",
      "fourth Player": "fourthPlayer",
      "fifthplayer": "fifthPlayer"
    }

这是我当前的python代码

def createTeam():
    username = 'user'
    password = 'pass'
    teamName = 'testTeam' #input('What is your teams name? ')
    firstPlayer = 'firstPlayer'#input('Who is your first Player: ')
    secondPlayer = 'secondPlayer'#input('Who is your second Player: ')
    thirdPlayer = 'thirdPlayer'#input('Who is your third Player: ')
    fourthPlayer = 'fourthPlayer'#input('Who is your fourth Player: ')
    FifthPlayer = 'fifthPlayer'#input('Who is your fifth Player: ')

    f = open("users.json", "r")
    users = json.load(f)

    x = users['users'].append({'username': username, 'password': password, 'team name': teamName, 'firstplayer': firstPlayer, 'secondplayer': secondPlayer, 'thirdplayer': thirdPlayer, 'fourth Player': fourthPlayer, 'fifthplayer': FifthPlayer})


    with open('users.json', 'w') as f:
        json.dump(x, f, indent=2)

简而言之,我只是想更新数据库中的记录,但我也不知道

2 个答案:

答案 0 :(得分:2)

Python将JSON转换为字典或列表。

在您的代码中,它似乎是字典,而不是列表-因此它没有append()-但我看不到文件中的完整数据来确认这一点。

(顺便说一句:append()不会创建新列表,因此您无法添加x = ...

f = open("users.json", "r")
users = json.load(f)

users['users']['username'] = username
users['users']['password'] = password
# ... etc. ...

现在您可以将users保存回来(而不是x

with open('users.json', 'w') as f:
    json.dump(users, f, indent=2)

答案 1 :(得分:0)

如果有

a = {
 "username": "user",
 "password": "password"
}

b = {
 "team name": "testTeam",
 "firstplayer": "firstPlayer",
 "secondplayer": "secondPlayer",
 "thirdplayer": "thirdPlayer",
 "fourth Player": "fourthPlayer",
 "fifthplayer": "fifthPlayer"
}

您可以使用

a.update(b)

获得

{
 "username": "user",
 "password": "password",
 "team name": "testTeam",
 "firstplayer": "firstPlayer",
 "secondplayer": "secondPlayer",
 "thirdplayer": "thirdPlayer",
 "fourth Player": "fourthPlayer",
 "fifthplayer": "fifthPlayer"
}

在您的特定情况下,您可以将代码更改为

users['users'].update({
    "team name": "testTeam",
    "firstplayer": "firstPlayer",
    "secondplayer": "secondPlayer",
    "thirdplayer": "thirdPlayer",
    "fourth Player": "fourthPlayer",
    "fifthplayer": "fifthPlayer"
}

但是,如果您实际上是在尝试更新字典中的现有用户,则可能需要这样的

users['users'][username].update({
 "team name": "testTeam",
 "firstplayer": "firstPlayer",
 "secondplayer": "secondPlayer",
 "thirdplayer": "thirdPlayer",
 "fourth Player": "fourthPlayer",
 "fifthplayer": "fifthPlayer"
})

或者如果您要添加用户

users['users'][username] = {
 "username": "user",
 "password": "password",
 "team name": "testTeam",
 "firstplayer": "firstPlayer",
 "secondplayer": "secondPlayer",
 "thirdplayer": "thirdPlayer",
 "fourth Player": "fourthPlayer",
 "fifthplayer": "fifthPlayer"
}