Skip to main content
 首页 » 编程设计

python之将 JSON 反序列化为对象

2025年05月04日17yjmyzz

我正在处理一个小问题,将一些 JSON(我无法更改格式,它不是我创建的服务)反序列化为 Python 对象。我已经设法使用 lambda 进行了转换,但我现在想使用 object_hook,看看是否可以使用 json.loads 方法进行转换。然而,这就是我现在失败的地方,我想知道是否有人可以为我指明正确的方向。

这是我目前拥有的代码:

import json 
 
class Light: 
    def __init__(self, id, name): 
        self.id = id 
        self.name = name 
 
response = '{"1": {"name": "bedroom"}, "2": {"name": "kitchen"}}' 
 
def object_decoder(obj): 
    return Light(......) 
 
print json.loads(response, object_hook=object_decoder) 

如您所见,response 是一个包含两个键的文档,分别命名为 1 和 2。如果我能让代码以 json 的方式工作,那就太好了.loads 会返回两个 Light 对象,但此刻,我卡住了,我不知道如何迭代响应来完成这项工作。

请您参考如下方法:

object_hook 不会帮助您,因为您在 json 字符串的不同级别上有 id 和 name:

object_hook, if specified, will be called with the result of every JSON object decoded and its return value will be used in place of the given dict.

让我们看看为什么 object_hook 不起作用。如果您打印即将到达 object_decoder 函数的对象,您会看到它从深处上升,如下所示:

{u'name': u'bedroom'} 
{u'name': u'kitchen'} 
{u'1': None, u'2': None} 
None 

这意味着您不能加入 object_decoder 调用以生成 Light 实例。

如何使用自定义 JSONDecoder改为上课:

import json 
 
 
class Light: 
    def __init__(self, id, name): 
        self.id = id 
        self.name = name 
 
 
response = '{"1": {"name": "bedroom"}, "2": {"name": "kitchen"}}' 
 
 
class Decoder(json.JSONDecoder): 
    def decode(self, s): 
        obj = super(Decoder, self).decode(s) 
        return [Light(id=k, name=v['name']) for k, v in obj.iteritems()] 
 
 
lights = json.loads(response, cls=Decoder) 
print lights  # prints [<__main__.Light instance at 0x9c3b50c>, <__main__.Light instance at 0x9c3b56c>] 
print [light.__dict__ for light in lights]  # prints [{'id': u'1', 'name': u'bedroom'}, {'id': u'2', 'name': u'kitchen'}] 

这其实和制作json.loads()然后实例化类是一样的