如何在python中向JSON列表添加字符串
以下是我的代码示例:如何在python中向JSON列表添加字符串,python,json,string,api,Python,Json,String,Api,以下是我的代码示例: try: REST_Call = Session.get(CC_URL_REST) #Getting the session for a particular url. REST_CALL = REST_Call.content #Retrieving the contents from the url. JSON_Data = json.loads(REST_CALL) #Loading data as JSON.
try:
REST_Call = Session.get(CC_URL_REST) #Getting the session for a particular url.
REST_CALL = REST_Call.content #Retrieving the contents from the url.
JSON_Data = json.loads(REST_CALL) #Loading data as JSON.
Report_JSON.append(JSON_Data) #Appending the data to an empty list
返回并附加到“Report_JSON”的JSON数据是:
[
{
"content": [
{
"id": 001,
"name": "Sample_Name",
"description": "Sample_description",
"status": "STARTED",
"source": null,
"user": "username"
}
},
],
我只想在上面的JSON列表中添加以下字符串格式的数据:
{
"CronExpression": "0 1,30 4,5,6,7,8,9,10,11,12,13,14 ? * 2,3,4,5,6"
},
上述字符串数据的示例代码:
Cron_Call = Session.get(Cron_URL_REST)
Cron_CALL = Cron_Call.content
Cron_Data = json.loads(Cron_CALL)
cron_value = Cron_Data["cronExpression"]
Report_JSON.append({
"CronExpression": cron_value
})
尝试将其附加到“Report_JSON”时,我得到的输出如下:
[
{
"content": [
{
"id": 001,
"name": "Sample_Name",
"description": "Sample_description",
"status": "STARTED",
"source": null,
"user": "username"
}
},
],
{
"CronExpression": "0 1,30 4,5,6,7,8,9,10,11,12,13,14 ? * 2,3,4,5,6"
},
我试图将两个数据显示在同一个“内容”选项卡下,而不是单独显示
这就是我想要得到的结果:
{
"id": 001,
"name": "Sample_Name",
"description": "Sample_description",
"status": "STARTED",
"source": null,
"user": "username"
"CronExpression": "0 1,30 4,5,6,7,8,9,10,11,12,13,14 ? * 2,3,4,5,6"
},
关于如何实现它,有什么想法吗?在这里,
Report_JSON
在Python中作为列表
类型加载(JSON数据可以被Python解释为列表
,如果它被[]
方括号包围,或者dict
如果它被{code>方括号包围)
调用Report\u JSON.append()
时,它将向列表中追加一个新项。您正在使用单个键值对(CronExpression
)创建一个新字典,并将其添加到列表中,这就是为什么这两个字典并排出现的原因
您应该做的是获取Report\u JSON
列表中的第一项,即字典;然后询问内容
键对应的值,这将是一个列表;然后询问列表中的第一项,这将是您要修改的词典(带有键id
,名称
,说明
,等等)
修改那本词典,然后把它放回列表中。下面是实现这一点的代码:
# Get first item in Report_JSON list
content_dict = Report_JSON[0]
# Get the value corresponding to the 'content' key, which is a list
content_value = content_dict['content']
# Get the first item in the list, which is the dict you want to modify
dict_to_modify = content_value[0]
# Now add your new key-value pair
dict_to_modify['CronExpression'] = "0 1,30 4,5,6,7 ..."
或者,要一次性完成:
Report_JSON[0]['content'][0]['CronExpression'] = "0 1,30 4,5,6,7 ..."
更新:如果“内容”列表有多个项目,您可以迭代该列表中的每个项目:
for content_dict in Report_JSON[0]['content']:
content_dict['CronExpression'] = "0 1,30 4,5,6,7 ..."
这将导致如下结果:
[
{
"content": [
{
"id": 001,
"name": "Sample_Name",
"description": "Sample_description",
"status": "STARTED",
"source": null,
"user": "username",
"CronExpression": "0 1,30 4,5,6,7 ..."
},
{
"id": 002,
"name": "Another_Sample_Name",
"description": "Another_sample_description",
"status": "STARTED",
"source": null,
"user": "another_username",
"CronExpression": "0 1,30 4,5,6,7 ..."
},
]
},
],
更新2:如果您对保留原始结构不感兴趣,并且希望剥离所有内容,包括“内容”键,您可以这样做开始:
Report_JSON = Report_JSON[0]['content']
而Report\u JSON
现在只是内部的“内容”列表。循环查看JSON\u数据['content']
,并为每个数据添加新键
Cron_Call = Session.get(Cron_URL_REST)
Cron_CALL = Cron_Call.content
Cron_Data = json.loads(Cron_CALL)
cron_value = Cron_Data["cronExpression"]
for x in JSON_DATA['content']:
x['CronExpression'] = cron_value
你能展示你想要的结果吗?这是一个很好的解释。这也奏效了。非常感谢,太好了!别忘了点击绿色复选标记接受这个答案,这样人们就知道它解决了你的问题。也看到