如何在 Bottle 中处理 JSON 请求?

发布于 2024-09-30 13:30:25 字数 465 浏览 2 评论 0原文

我需要从 JSON 获取数据,通过 Ajax 从客户端传输。基本上我使用了这样的东西:

@route('/ajax')
def serve_ajax():
    return main.parse_request(json.dumps(dict(request.GET)))

其中 main.parse_request 是一个函数,它包含一些处理 JSON 中的变量的逻辑(它是我们游戏引擎的主要过程)。

所以问题是我无法通过在字典中转换 request.GET 来正确处理 JSON 变量:因为以我已经编写的方式,我无法传递嵌套对象和数组。此外,每个值都有一个字符串类型,而我需要在整数上有整数类型,在其他值上有字符串类型。

或者,既然我可以获取原始查询字符串(通过 request.query_string),那么如何将查询字符串转换为原始 JSON 对象?

I need to get data from JSON, transferred by Ajax from the client. Basically I used something like this:

@route('/ajax')
def serve_ajax():
    return main.parse_request(json.dumps(dict(request.GET)))

Where main.parse_request is a function, that contains some logics to deal with variables in JSON (it is a main procedure of our game engine).

So the problem is that I can't correctly handle JSON variables, by transforming request.GET in a dict: because in a way that I already wrote I can't pass nested objects and arrays. Also every value has a string type, while I need to have integer types on integers and string type on rest other.

Or, since I can obtain the original query string (by request.query_string), how can I convert a query string into an original JSON object?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

破晓 2024-10-07 13:30:25

现在,从 Bottle 0.10 开始,request.json 就可以使用了:)

文档是 此处

Now since bottle 0.10, the request.json is ready for use :)

Document is here.

寄居者 2024-10-07 13:30:25

request.json限制MEMFILE_MAX

如果请求数据大于 MEMFILE_MAX,则另一种方法有效

json.load(request.body)

request.json is limited by MEMFILE_MAX.

Another way works if request data is larger than MEMFILE_MAX

json.load(request.body)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文