這可能聽起來像一個普通的問題,但我還沒有找到一個很好的答案,我正在嘗試做什麼。如何在Python中添加JSON中的鍵/值?
採取d.json:
{"SDA":{"Info":{"Description":"Anti Advertisment Bot, Blocks invites extensively.","Download Link":"http://sda.khionu.net/docs/, http://sda.khionu.net/docs/"}}, "Unit 02":{"Info":{"Description":"Server logging bot, c!serverlogs 'server name here if spaces' <# 1-9999>","Download Link":"https://discordapp.com/oauth2/authorize?client_id=222881716606468096&scope=bot&permissions=32768"}}}
我試圖把它添加到它,以逗號分隔:
{'Ctest': {'Info': {'Description': 'Hi', 'Download Link': 'Sure'}}}
我試過多種方式來做到這一點,但沒有工作。這裏是我當前的代碼
a = d[toolname] = {str(toolname):{"Info":{"Description": tooldesc, "Download Link": toollink}}}
f.write(str(a))
f.close()
return jsonify(a), 201
我的整個目標是編寫
{'Ctest': {'Info': {'Description': 'Hi', 'Download Link': 'Sure'}}}
到d.json這樣
{"SDA":{"Info":{"Description":"Anti Advertisment Bot, Blocks invites extensively.","Download Link":"http://sda.khionu.net/docs/, http://sda.khionu.net/docs/"}}, "Unit 02":{"Info":{"Description":"Server logging bot, c!serverlogs 'server name here if spaces' <# 1-9999>","Download Link":"https://discordapp.com/oauth2/authorize?client_id=222881716606468096&scope=bot&permissions=32768"}}, {'Ctest': {'Info': {'Description': 'Hi', 'Download Link': 'Sure'}}}
你還沒有找到解析JSON和製作Python字典的好參考嗎? –
您需要使用方括號''在JSON中'表示一個值數組 - 大括號'{'用於對象(命名屬性) – developius