在python中为json.dump编写嵌套字典

时间:2016-06-28 10:07:34

标签: python json dictionary

我有一组python列表,我希望在结构中的json文件中连续写入,如下所示

[

{
    "_id": {
        "$oid": "5707b5f4e4b0c4265caf3c87"
    },
    "TimeStamp": 1,
    "TraceData": [
        {
            "data": {
                "y": 443.732,
                "angle": 1.11416,
                "speed": 1.42906,
                "ObjectType": "Pedestrians",
                "x": 217.991,
                "D2D": "DUE_1_2"
            },
            "id": "DUE_1_1"
        },
        {
            "data": {
                "y": 571.965,
                "angle": 1.22555,
                "speed": 1.18132,
                "ObjectType": "Pedestrians",
                "x": 205.708,
                "D2D": "DUE_20_1"
            },
            "id": "DUE_20_2"
        }
    ]
},
{
    "_id": {
        "$oid": "5707b5a8e4b0a37fb1a38c57"
    },
    "TimeStamp": 2,
    "TraceData": [
        {
            "data": {
                "y": 419.936,
                "angle": 1.21995,
                "speed": 1.38648,
                "ObjectType": "Pedestrians",
                "x": 153.693,
                "D2D": "DUE_1_2"
            },
            "id": "DUE_1_1"
        },
        {
            "data": {
                "y": 571.143,
                "angle": 1.0939,
                "speed": 1.31394,
                "ObjectType": "Pedestrians",
                "x": 295.097,
                "D2D": "DUE_20_1"
            },
            "id": "DUE_20_2"
        }
    ]
}

]

我有每个变量的python列表('y','x','angle','speed'等)。我在python中创建了嵌套字典,通过FOR循环编写这些列表。代码如下

 for eachdata in range(index-1): 
    OuterDict['TimeStamp']['TraceData']['data']['x'] = lat[eachdata]
    OuterDict['TimeStamp']['TraceData']['data']['y'] = long[eachdata]
    OuterDict['TimeStamp']['TraceData']['data']['angle'] = angle[eachdata]
    OuterDict['TimeStamp']['TraceData']['data']['speed'] = speed[eachdata]
    OuterDict['TimeStamp']['TraceData']['data']['ObjectType'] = ObjectType[eachdata]
index = 0


out_file = open("klsimulationjson.js","w")

json.dump(OuterDict,out_file,indent = 4)

out_file.close()

此代码产生以下结果。我无法搞清楚 1)如何在类似结构中迭代填充字典 2)为键'TimeStamp'添加值# 3)创建密钥 - 'id'

{
"TimeStamp": {
    "TraceData": {
        "data": {
            "x": "7.739439",
            "speed": "6.072069",
            "y": "49.421938",
            "ObjectType": "Bus",
            "angle": "68.576206"
        }
    }
}
}

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

我假设这是你想要的,因为你没有dict {}持有没有键的值:

[{
    "TimeStamp": 1,
    "TraceData": [{
        "data": {
            "y": 443.732,
            "angle": 1.11416,
            "speed": 1.42906,
            "ObjectType": "Pedestrians",
            "x": 217.991,
            "D2D": "DUE_1_2"
        },
        "id": "DUE_1_1"
    }, {
        "data": {
            "y": 430.645,
            "angle": 1.07287,
            "speed": 1.41977,
            "ObjectType": "Pedestrians",
            "x": 234.104,
            "D2D": "DUE_1_1"
        },
        "id": "DUE_1_2"
    }, {
        "data": {
            "y": 362.25,
            "angle": 1.43214,
            "speed": 1.44059,
            "ObjectType": "Pedestrians",
            "x": 50.5509,
            "D2D": "DUE_2_2"
        },
        "id": "DUE_2_1"
    }]
}, {
    "TimeStamp": 2,
    "TraceData": [{
        "data": {
            "y": 443.732,
            "angle": 1.11416,
            "speed": 1.42906,
            "ObjectType": "Pedestrians",
            "x": 217.991,
            "D2D": "DUE_1_2"
        },
        "id": "DUE_1_1"
    }, {
        "data": {
            "y": 430.645,
            "angle": 1.07287,
            "speed": 1.41977,
            "ObjectType": "Pedestrians",
            "x": 234.104,
            "D2D": "DUE_1_1"
        },
        "id": "DUE_1_2"
    }, {
        "data": {
            "y": 362.25,
            "angle": 1.43214,
            "speed": 1.44059,
            "ObjectType": "Pedestrians",
            "x": 50.5509,
            "D2D": "DUE_2_2"
        },
        "id": "DUE_2_1"
    }]
}]

所以,基本上是一个列表[]的dicts {}(#1),每个都有一个标识“TimeStamp”,一个“TraceData”列表[]也包含dicts {{ 1}}(#2),每个都包含“id”和“data”。

您的代码:

{}

查看单个dict(类型#1),然后执行几个错误:

  1. “TimeStamp”应该是Trace帧的标识符,如整数或字符串,而不是dict for eachdata in range(index-1): OuterDict['TimeStamp']['TraceData']['data']['x'] = lat[eachdata] OuterDict['TimeStamp']['TraceData']['data']['y'] = long[eachdata] OuterDict['TimeStamp']['TraceData']['data']['angle'] = angle[eachdata] OuterDict['TimeStamp']['TraceData']['data']['speed'] = speed[eachdata] OuterDict['TimeStamp']['TraceData']['data']['ObjectType'] = ObjectType[eachdata] ,但无论如何都可以将其作为dict访问。
  2. “TraceData”是一个列表{},因此您需要对其进行访问 - 它可能包含多个dict [](#2),实际上它在您的示例中也是如此。
  3. 您访问“数据”字典并写入相关信息,但是再一次,您只针对一个字典#1和仅针对一个字典#2,因此结果似乎只包含其中一个。
  4. 这是在正确构建时,或多或少地访问它的方式,以便您可以了解如何修复代码:

    {}