python - 将 JSON 反序列化为对象

标签 python json deserialization

我正在处理一个小问题,将一些 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 字符串的不同级别上有 idname:

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()然后实例化类是一样的

关于python - 将 JSON 反序列化为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18579882/

相关文章:

javascript - 动态D3力图

javascript - 基于 JSON 回调数据的重定向 URL

java - Gson时间反序列化

c# - Tuple<...> 在另一种类型中的 Json.NET 反序列化不起作用?

python - 如何在 python 中对图像序列(具有不同的扩展名)进行排序

python - 从数据提取中删除字母只留下数字Python

java - 使用正则表达式解析一个非常简单的 JSON 文件

python - Bash 使用来自 Anaconda 的 Python,即使它已被停用

python - ConfigObj 和单元素列表

java - 在不知道对象数量的情况下反序列化具有多个对象的文件