【问题标题】:How to set json objects in redis 4.0?如何在 redis 4.0 中设置 json 对象?
【发布时间】:2022-01-12 00:54:23
【问题描述】:

截至redis-py4.0,支持json。我在Windows 11 上使用redis-py 版本4.0.2。我有一个使用redislabs/rejson:latest在docker中运行的redis服务器。

我正在尝试通过以下方式加载 json 对象 -

import redis
from redis.commands.json.path import Path
from redis import exceptions
from redis.commands.json.decoders import unstring, decode_list

r = redis.Redis()
d = {"hello": "world", b"some": "value"}
r.json().set("somekey", Path.rootPath(), d)

在测试中它是如何完成的 - https://github.com/redis/redis-py/blob/e8bcdcb97b8c915e2bb52353aeda17c00e1be215/tests/test_json.py#L19

但是我收到了这个错误 -

TypeError: keys must be str, int, float, bool or None, not bytes

我错过了什么?

【问题讨论】:

    标签: python redis redis-py


    【解决方案1】:

    你的 json 中有一个 b 前缀,代表字节。

    只需从b"some": 中删除'b',它就会按预期工作:

    d = {"hello": "world", "some": "value"}
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-26
    • 2021-11-17
    • 1970-01-01
    • 2012-08-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-07
    • 2011-05-05
    相关资源
    最近更新 更多