Python 在列表中访问不同的JSON键/值对
不确定我是否在努力实现不可能的目标?我有一个JSON字符串:Python 在列表中访问不同的JSON键/值对,python,json,python-3.x,dictionary,nested,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"
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)
并得到了空列表:
[]
[]
[]
[]
提前感谢您需要循环每个设备的
记录
元素。然后将设备中的字段与每条记录合并
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)
您需要在每个设备的
记录
元素上循环。然后将设备中的字段与每条记录合并
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)
可能是正确的-但我相信,如果您通过创建表示数据结构中项目的对象层次结构来解除代码的使用,您将有一个轻松得多的时间:
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
for device in some_object.devices:
print(device.id, device.name, device.records[0].timestamp, device.records[0].group, device.records[0].value)
如果您走这条路线,就可以轻松地将整个JSON解析为强类型对象:
some_object = SomeObject(dot[0]["type"], dot[0]["date"], dot[0]["device"])
然后,报告数据结构的特定部分应该相当容易/直接:
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
for device in some_object.devices:
print(device.id, device.name, device.records[0].timestamp, device.records[0].group, device.records[0].value)
可能是正确的-但我相信,如果您通过创建表示数据结构中项目的对象层次结构来解除代码的使用,您将有一个轻松得多的时间:
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
for device in some_object.devices:
print(device.id, device.name, device.records[0].timestamp, device.records[0].group, device.records[0].value)
如果您走这条路线,就可以轻松地将整个JSON解析为强类型对象:
some_object = SomeObject(dot[0]["type"], dot[0]["date"], dot[0]["device"])
然后,报告数据结构的特定部分应该相当容易/直接:
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
for device in some_object.devices:
print(device.id, device.name, device.records[0].timestamp, device.records[0].group, device.records[0].value)
如果isinstance(p,list)始终为false,请检查为什么
;可能通过将您的理解展开到经典的循环中来实现:您的输入数据在字符串中有换行符,因此没有人可以按原样复制/粘贴以进行测试。请更正。谢谢@Jean Françoisfar不知道你的意思。不过,问题似乎已经解决了。如果isinstance(p,list)
始终为false,则感谢检查为什么;可能通过将您的理解展开到经典的循环中来实现:您的输入数据在字符串中有换行符,因此没有人可以按原样复制/粘贴以进行测试。请更正。谢谢@Jean Françoisfar不知道你的意思。不过,问题似乎已经解决了。感谢Hanks@Barmar,这真是一种享受。我看到的东西太复杂了。谢谢你的简化。Cheers Hanks@Barmar真是太棒了。我看到的东西太复杂了。谢谢你的简化。Cheesthanks@alex,是的,我同意,这可以完全控制JSON的所有元素。我在组相关值中添加了“{}”。是否有一种方法可以对值进行排序,以便转换为数据帧?[{'17','20165cf4e596','2018-12-02T00:40:00.499+00:00','887','undefined'},{1063','18','2018-12-02T00:42:00.499+00:00','5F401A6F66','undefined'},{'19','2018-12-02T00:44:00.499+00:00','569bb0147a72',817','undefined'}是的。我现在还不知道,但是你可能可以在一些对象上使用sorted()
和lambdas。设备
。谢谢@alex,一定会尝试的。非常感谢!我认为最终的目标是将其转换成类似CSV的东西,这就是将所有内容都展平的目的。@Barmar完全合理:)谢谢@alex,同意,这可以完全控制JSON的所有元素。我在组相关值中添加了“{}”。是否有一种方法可以对值进行排序,以便转换为数据帧?[{'17','20165cf4e596','2018-12-02T00:40:00.499+00:00','887','undefined'},{1063','18','2018-12-02T00:42:00.499+00:00','5F401A6F66','undefined'},{'19','2018-12-02T00:44:00.499+00:00','569bb0147a72',817','undefined'}是的。我现在还不知道,但是你可能可以在一些对象上使用sorted()
和lambdas。设备
。谢谢@alex,一定会尝试的。非常感谢!我认为最终的目标是把它变成一个类似CSV的东西,这就是把所有东西都压扁的目的。@Barmar完全合理:)