【问题标题】:Python: "import JSON... json.loads(request.body)" 2.7->3.4Python:“导入 JSON...json.loads(request.body)”2.7->3.4
【发布时间】:2014-09-06 10:03:03
【问题描述】:

我最近将 django 服务器从 python 版本 2.7 转换为 python 版本 3.4.1。 我的 request.body 是一个序列化为 JSON 的数组。反序列化后,它将是一个 python 列表。

不幸的是,json.loads 似乎不再需要原始字节(这就是 request.body 的内容)。

我该如何解决这个问题?

def index(request):
    if request.method == 'POST':

        print("Made it here!")
        registered = []
        notRegistered = []
        print("Is it this?")

        print(repr(request.body))

        data = json.loads(request.body)

        print("Did I make it here?")

最后一次调用 print 永远不会执行,这就是为什么我假设它与 json.loads() 相关

【问题讨论】:

    标签: python json django python-2.7 python-3.x


    【解决方案1】:

    我希望会发生回溯,而不仅仅是“最后一行永远不会执行”,但除此之外......

    # Let's just assume the request is UTF-8 encoded.
    data = json.loads(request.body.decode('utf-8'))
    

    【讨论】:

    • 在我的情况下,我有 request.body = str: json=%7B%22layers%22%3A%7B%22BackgroundLayer%22%3**,而 json.loads 给了我错误 ValueError:无法解码 JSON 对象,为什么?
    猜你喜欢
    • 2016-07-27
    • 1970-01-01
    • 2014-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-13
    • 1970-01-01
    相关资源
    最近更新 更多