在列表理解中访问不同的JSON键/值对

时间:2018-12-18 18:36:06

标签: python json python-3.x dictionary nested

不确定我是否试图实现不可能?我有这个JSON字符串:

dot= [{"type": 1, "date": "2018-12-02T00:40:03.2186792+00:00", "device": 
[{"id": "20165cf4e596", "deviceName": "17", "records": [{"timestamp": "2018- 
12-02T00:40:00.499+00:00", "grp": "undefined", "val": 887}]}, {"id": 
"5f401a6a6f66", "deviceName": "18", "records": [{"timestamp": "2018-12- 
02T00:42:00.499+00:00", "grp": "undefined", "val": 1063}, {"timestamp": 
"2018-12-02T00:41:00.498+00:00", "grp": "undefined", "val": 907}]}, {"id": 
"569bb0147a72", "deviceName": "19", "records": [{"timestamp": "2018-12- 
02T00:44:00.499+00:00", "grp": "undefined", "val": 817}, {"timestamp": 
"2018-12-02T00:43:00.498+00:00", "grp": "undefined", "val": 1383}]}, {"id": 
"ef829aa3", "deviceName": "2", "records": [{"timestamp": "2018-12- 
02T00:46:00.499+00:00", "grp": "undefined", "val": 1173}]}, {"id": 
"388ae8f2fa64", "deviceName": "17", "records": [{"timestamp": "2018-12- 
02T00:41:00.499+00:00", "grp": "undefined", "val": 866}, {"timestamp": 
"2018-12-02T00:32:00.492+00:00", "grp": "undefined", "val": 1080}]}, {"id": 
"01f874b30b55", "deviceName": "19", "records": [{"timestamp": "2018-12- 
02T00:43:00.499+00:00", "grp": "undefined", "val": 1050}, {"timestamp": 
"2018-12-02T00:42:00.498+00:00", "grp": "undefined", "val": 1084}]}]}]

我想实现以下目标:

[{'id': '20165cf4e596','deviceName': '17','timestamp': '2018-12- 
02T00:40:00.499+00:00','grp': 'undefined','val': 887},
{'id': '5f401a6a6f66','deviceName': '18','timestamp': '2018-12- 
02T00:42:00.499+00:00','grp': 'undefined','val': 1063},
{'id': '5f401a6a6f66','deviceName': '18','timestamp': '2018-12- 
02T00:41:00.498+00:00','grp': 'undefined','val': 907},...]

我使用了以下代码:

for i in dot:
    for k in i['device']:
        d2= [[{l:m},{'value':v}] for l,m in k.items() for p in m if 
        isinstance(p,list) for v in p]
        print(d2)

并且有空列表:

[]
[]
[]
[]

预先感谢

2 个答案:

答案 0 :(得分:2)

您需要遍历每个设备的records元素。然后将设备中的字段与每个记录结合起来。

result = []
for i in dot:
    for k in i['device']:
        for r in k['records']:
            result.append({"id": k["id"], "deviceName": k["deviceName"], "timestamp": r["timestamp"], "grp": r["grp"], "val": r["val"]})
print(result)

答案 1 :(得分:0)

Barmar's answer可能是正确的-但我相信,如果通过创建代表数据结构中项目的对象层次结构来对代码进行模糊处理,您将拥有更加轻松的时间:

class SomeObject:
    def __init__(self, some_object_type, date, devices):
        self.type = some_object_type
        self.date = date
        self.devices = []

        for d in devices:
            some_device = SomeDevice(d["id"], d["deviceName"], d["records"])
            self.devices.append(some_device)

class SomeDevice:
    def __init__(self, some_device_id, name, records):
        self.id = some_device_id
        self.name = name
        self.records = []

        for r in records:
            some_record = SomeDeviceRecord(r["timestamp"], r["grp"], r["val"])
            self.records.append(some_record)

class SomeDeviceRecord:
    def __init__(self, timestamp, group, value):
        self.timestamp = timestamp
        self.group = group
        self.value = value

如果走这条路,您可以轻松地将整个JSON解析为强类型的对象:

some_object = SomeObject(dot[0]["type"], dot[0]["date"], dot[0]["device"])

然后应该很容易/简单地报告数据结构的特定部分:

for device in some_object.devices:
    print(device.id, device.name, device.records[0].timestamp, device.records[0].group, device.records[0].value)