如何从Python中删除JSON中的括号?

时间:2015-07-27 01:05:57

标签: python json dictionary

我正在使用json.dumps将字典列表加载到JSON对象中。我的输出类似于:

[
    {
        "MetaData": {},
        "SRData": {
            "ListOfLa311DeadAnimalRemoval": {
                "DeadAnimalRemoval": [
                    {
                        "DACItemCount": "0",
                        "DACType": " ",
                        "DriverFirstName": "SA",
                        "DriverLastName": "Aguilar",
                        "LastUpdatedBy": "SANSTAR1",
                        "Name": "070920151119458601",
                        "Type": "Dead Animal Removal"
                    },
                    {
                        "DACType": " ",
                        "DriverFirstName": "SA",
                        "DriverLastName": "Aguilar",
                        "LastUpdatedBy": "SANSTAR1",
                        "Type": "Dead Animal Removal"
                    }
                ]
            },
            "ReasonCode": "",
            "ResolutionCode": "A",
            "SRNumber": "1-20979881"
        }
    }
]

如何成功删除JSON对象开头和结尾的括号?

附加词典和列表的代码:

    dL311 = dict()
    dL311.setdefault("DeadAnimalRemoval", l311)
    dResult.setdefault("ListOfLa311DeadAnimalRemoval",dL311)

    #Ends of adding additional itmes ****************************************
    lResults.append({"MetaData": {}, "SRData": dResult})

    ii = ii + 1
    print(json.dumps(lResults, sort_keys=True, indent=4))

2 个答案:

答案 0 :(得分:1)

只需序列化字典:

textColor

答案 1 :(得分:1)

括号表示JSON数组,在您的示例中包含一个元素。在Python中,只需选择根数组的第一个元素并转换回JSON。

import json
data = json.loads('[...]')
str = json.dumps(data[0])