Python 金字塔解析 json

Python Pyramid parse json

我正在 posting json 到 Python 金字塔服务器,但我无法在服务器端解析它。

post 请求如下所示:

$.ajax({
url: "http://localhost:6543/linefollower/7/send_result", 
type: "POST",
data: '{"results": [{"robot_name": "Satikas", "result": null, "team_nr": 30, "team_name": "IT Vennad", "id": 57}]}',
contentType: "application/json; charset=utf-8",
dataType: "json"
}

但是在服务器端,我在打印时收到了这个消息(request.body)

b'%5B%7B%22robot_name%22%3A+%22Satikas%22%2C+%22result%22%3A+null%2C+%22team_nr%22%3A+30%2C+%22team_name%22%3A+%22IT+Vennad%22%2C+%22id%22%3A+57%7D%5D='

我应该怎么做才能将 posted 的内容解析为 JSON? Pyramid 的 request.json_body 何时应包含已解析的 json?

我没有完美的答案,但我可以告诉你这与编码有关。 str.encode() 或 u'str 也许吧。你可以开始往这个方向看。

尝试在发送前明确地将数据序列化为 JSON:

$.ajax({
    url: "http://localhost:6543/linefollower/7/send_result", 
    type: "POST",
    data: JSON.stringify({"results": [... "team_name": "IT Vennad", "id": 57}]}),
    contentType: "application/json; charset=utf-8",
    dataType: "json"
}

您的数据对象是一个字符串。它应该只是一个对象。删除引号

data: {"results": [{"robot_name": "Satikas", "result": null, "team_nr": 30, "team_name": "IT Vennad", "id": 57}]},