使用python在json中添加方括号

时间:2017-03-09 09:57:38

标签: python json

我是一个json,如下所示。我只是想在关键活动中添加方括号的一部分'。但我是这个python编码的新手,所以我没有采用任何方法在JSON中使用python为特定组件插入方括号。

{
    "name": "CopyPipeline-rdc",
    "properties": {
        "end": "2017-02-14T00:00:00Z",
        "start": "2017-02-14T00:00:00Z",
        "activities": {
            "name": "CopyActivity-0",
            "type": "Copy",
            "input": {
                "name": "InputDataset-rdc"
            },
            "output": {
                "name": "OutputDataset-rdc"
            },
            "policy": {
                "retry": 0,
                "timeout": "00:00:00",
                "concurrency": 1,
                "executionpriorityorder": "OldestFirst"
            },
            "scheduler": {
                "style": "StartOfInterval",
                "interval": "1",
                "frequency": "Day"
            },
            "typeproperties": {
                "source": {
                    "type": "BlobSource",
                    "recursive": false
                },
                "sink": {
                    "type": "BlobSink",
                    "writeBatchSize": 0,
                    "writeBatchTimeout": "00:00:00"
                }
            }
        },
        "expiration": "15.00:00:00",
        "description": "''",
        "pipelinemode": "OneTime"
        }
    }

1 个答案:

答案 0 :(得分:2)

您不能简单地将方括号“添加”到JSON输出的一部分。方括号用于表示数组,列表或其他可枚举的对象序列。

为了实现您所请求的内容,您必须将activities属性(或字典条目)修改为列表,元组或其他适当的序列类型。

示例代码:

>>> import json
>>> obj = {"activities": [{"name": "CopyActivity-0",
...                        "type": "Copy",
...                        "input": {"name": "InputDataset-rdc"}
...                        }]    # note that we used [] to create an array
...       }
>>> print json.dumps(obj, indent=2)
{
  "activities": [
    {
      "input": {
        "name": "InputDataset-rdc"
      },
      "type": "Copy",
      "name": "CopyActivity-0"
    }
  ]
}