【问题标题】:How to add a string to JSON list in python如何在python中将字符串添加到JSON列表
【发布时间】:2020-04-11 14:40:15
【问题描述】:

这是我的代码示例:

    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"
        },

关于如何实现它的任何想法?

【问题讨论】:

  • 你能展示你想要的结果吗?

标签: python json string api


【解决方案1】:

循环JSON_Data['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

【讨论】:

    【解决方案2】:

    这里,Report_JSON 在 Python 中被加载为 list 类型(如果 JSON 数据被 [] 方括号包围,则 Python 可以将 JSON 数据解释为 list,或者 dict 如果它被{} 大括号包围)。

    当您调用Report_JSON.append() 时,它会在列表中添加一个新项目。您正在使用单个键值对 (CronExpression) 创建一个新字典并将其添加到列表中,这就是两个字典并排放置的原因。

    您应该做的是获取Report_JSON 列表中的第一项,即字典;然后询问与content 键对应的值,这将是一个列表;然后询问该列表中的第一项,这将是您要修改的字典(使用键 idnamedescription 等)

    修改那个字典,然后把它放回列表中。这是执行此操作的代码:

    # 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_JSON 现在只是内部“内容”列表。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-12
    • 2022-07-28
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2023-02-15
    • 2015-07-31
    相关资源
    最近更新 更多