{
"blogs": [
{
"header": "Welcome",
"author": "Auriga",
"team" : "Webmaster",
"date" : ["2015", "12", "12"],
"paragraphs" : [
"Blah blah blah"
],
"images": []
}
],
基本上我想要它以相同的格式添加另一个标题,作者,团队,日期,段落和图像。
此外,我需要在“博客”部分内进行。
谢谢!
编辑*:另外如何在标题旁边发布文字,例如。
答案 0 :(得分:0)
编写示例代码非常重要,它可以突出您的问题,但不会陷入无关紧要的细节中。通过这个例子,我们可以更好地理解这个问题,并且可以在没有太多额外解释的情况下编写答案。
在你的情况下,问题是令人费解的,因为你不直接更新JSON(它只是一个字符串) - 你将它加载到python并更新python对象。还有很多其他问题,比如这个json来自哪里,但它们并不是更新问题的核心。
我冒昧地写了一个我希望是正确的例子
import json
json_str = """{
"blogs": [
{
"header": "Welcome",
"author": "Auriga",
"team" : "Webmaster",
"date" : ["2015", "12", "12"],
"paragraphs" : [
"Blah blah blah"
],
"images": []
}
]}"""
data = json.loads(json_str)
newblog = {
"header": "Welcome",
"author": "Auriga",
"team": "Webmaster",
"date": ["2015", "12", "12"],
"paragraphs" : [
"Blah blah blah"
],
"images": []
}
# THE PROBLEM: How do I add the new blog to blogs?
????
print(json.dumps(data, indent=' '))
答案只是data['blogs'].append(newblog)
import json
json_str = """{
"blogs": [
{
"header": "Welcome",
"author": "Auriga",
"team" : "Webmaster",
"date" : ["2015", "12", "12"],
"paragraphs" : [
"Blah blah blah"
],
"images": []
}
]}"""
data = json.loads(json_str)
newblog = {
"header": "Welcome",
"author": "Auriga",
"team": "Webmaster",
"date": ["2015", "12", "12"],
"paragraphs" : [
"Blah blah blah"
],
"images": []
}
# THE PROBLEM: How do I add the new blog to blogs?
data['blogs'].append(newblog)
print(json.dumps(data, indent=' '))