【问题标题】:Return HTTP json response返回 HTTP json 响应
【发布时间】:2020-12-21 02:11:13
【问题描述】:

我是使用 python 开发 REST API 的新手。 我想将此列表作为 json 返回作为响应。我使用来自 sanic 的响应。

response_data = [{'error': False, 'errormsg': '', 'status': '200'}, {'data': {1: {'name': 'sosialisasi', 'm_value': 77, 'c_value': 876, 'cu_value': 568, 'cl_value': 468, 'independent_vars': {'jumlah hari': {None: {'val_name': None, 'value': None}}, 'tingkatan sosialisasi': {2: {'val_name': 'kecamatan', 'value': '0.5'}, 1: {'val_name': 'kabupaten', 'value': '0.75'}}}}}}]

response.json(response_data, 200)

但我收到错误TypeError: expected bytes, str found

Traceback (most recent call last):


File "/home/dewi/saniccrudenv/lib/python3.8/site-packages/sanic/app.py", line 939, in handle_request
    response = await response
  File "/home/dewi/anomali/operations.py", line 183, in getAsb
    return response.json(response_data, 200)
  File "/home/dewi/saniccrudenv/lib/python3.8/site-packages/sanic/response.py", line 210, in json
    dumps(body, **kwargs),
TypeError: expected bytes, str found

我的代码有问题吗? 当我在 response_data 上使用 json.dumps 时,这不是错误。 但它返回这样的json字符串

"[{\"error\": false, \"errormsg\": \"\", \"status\": \"200\"}, {\"data\": {\"1\": {\"name\": \"sosialisasi\", \"m_value\": 77, \"c_value\": 876, \"cu_value\": 568, \"cl_value\": 468, \"independent_vars\": {\"jumlah hari\": {\"null\": {\"val_name\": null, \"value\": null}}, \"tingkatan sosialisasi\": {\"2\": {\"val_name\": \"kecamatan\", \"value\": \"0.5\"}, \"1\": {\"val_name\": \"kabupaten\", \"value\": \"0.75\"}}}}}}]"

我想要的是json对象,类似这样的

[
{
    "error": false,
    "errormsg": "",
    "status": "200"
},
{
    "data": [
        {
            "id": 1,
            "nama": "test1",
            "kode": "101"
        },
        {
            "id": 2,
            "nama": "test2",
            "kode": "202"
        }
    ]
}

]

【问题讨论】:

  • 能否请您发布完整的回溯?
  • 我已经用回溯编辑了问题
  • 你安装了ujson吗?问题可能就在那里,因为 sanic 会首先尝试导入 ujson 并使用它来转储 json。
  • 不,我在没有 ujson 的情况下安装了 sanic
  • 我添加了import ujson,它现在正在工作。感谢您的帮助

标签: python sanic


【解决方案1】:

问题是你有None作为关键字:

response_data = [
        {"error": False, "errormsg": "", "status": "200"},
        {
            "data": {
                1: {
                    "name": "sosialisasi",
                    "m_value": 77,
                    "c_value": 876,
                    "cu_value": 568,
                    "cl_value": 468,
                    "independent_vars": {
                        "jumlah hari": {
                            None: {"val_name": None, "value": None}
                        },
                        "tingkatan sosialisasi": {
                            2: {"val_name": "kecamatan", "value": "0.5"},
                            1: {"val_name": "kabupaten", "value": "0.75"},
                        },
                    },
                }
            }
        },
    ]

如果你改变:

"jumlah hari": {
    None: {"val_name": None, "value": None}
},

"jumlah hari": {
    "None": {"val_name": None, "value": None}
},

它将按预期工作。

$ curl localhost:9999/                                                                                                                                          (env: sanic) 
[{"error":false,"errormsg":"","status":"200"},{"data":{"1":{"name":"sosialisasi","m_value":77,"c_value":876,"cu_value":568,"cl_value":468,"independent_vars":{"jumlah hari":{"None":{"val_name":null,"value":null}},"tingkatan sosialisasi":{"2":{"val_name":"kecamatan","value":"0.5"},"1":{"val_name":"kabupaten","value":"0.75"}}}}}}]

JSON 规范:

对象结构表示为一对围绕零个或多个名称/值对的花括号标记。名称是一个字符串。

Source

【讨论】:

    猜你喜欢
    • 2017-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多