Json对象的几个对象

时间:2019-02-15 13:41:04

标签: python json python-3.x

我想创建一个JSON

{
    "beacons": {
        "0c:f3:ee:16:6f:15": {
            "label": "label",
            "major": "7",
            "minor": 15349,
            "uuid": "699EBC80-E1F3-11E3-9A0F-0CF3EE3BC012"
        },
        "0c:f3:ee:16:6f:24": {
            "label": "label",
            "major": "7",
            "minor": 15364,
            "uuid": "699EBC80-E1F3-11E3-9A0F-0CF3EE3BC012"
        },
        "0c:f3:ee:16:6f:53": {
            "label": "label",
            "major": "7",
            "minor": 15411,
            "uuid": "699EBC80-E1F3-11E3-9A0F-0CF3EE3BC012"
        }
    }
}

我已经创建了这样的信标对象

beacon = {
    advertiser_mac: {
        'major': major,
        'minor': minor,
        'uuid': proximity_uuid
    }
}

但是我不知道如何像上面的示例一样将所有这些对象合并到一个对象中

1 个答案:

答案 0 :(得分:1)

beacon是字典。因此,如果您知道如何为每个advertiser_mac生成字典,则只需进行键分配即可:

beacon = {}

advertiser_mac = "0c:f3:ee:16:6f:53"
advertiser_mac_dict = {
            "label": "label",
            "major": "7",
            "minor": 15411,
            "uuid": "699EBC80-E1F3-11E3-9A0F-0CF3EE3BC012"
        }

beacon[advertiser_mac] = advertiser_mac_dict

beacon
{"0c:f3:ee:16:6f:53": {
            "label": "label",
            "major": "7",
            "minor": 15411,
            "uuid": "699EBC80-E1F3-11E3-9A0F-0CF3EE3BC012"
        }
}