Python 如何在 Bottle 中处理 JSON 请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4058335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I handle a JSON request in Bottle?
提问by kravitz
I need to get data from JSON, transferred by Ajax from the client. Basically I used something like this:
我需要从 JSON 获取数据,通过 Ajax 从客户端传输。基本上我使用了这样的东西:
@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).
其中 main.parse_request 是一个函数,它包含一些处理 JSON 变量的逻辑(它是我们游戏引擎的一个主要程序)。
So the problem is that I can't correctly handle JSON variables, by transforming request.GETin 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.
所以问题是我无法通过request.GET在 dict 中进行转换来正确处理 JSON 变量:因为以我已经编写的方式,我无法传递嵌套对象和数组。此外,每个值都有一个字符串类型,而我需要在整数上有整数类型,在其他上有字符串类型。
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?
或者,由于我可以获得原始查询字符串(by request.query_string),我如何将查询字符串转换为原始 JSON 对象?
采纳答案by Felix Yan
回答by Like
request.jsonis limited by MEMFILE_MAX.
request.json受MEMFILE_MAX限制。
Another way works if request data is larger than MEMFILE_MAX
如果请求数据大于 MEMFILE_MAX,另一种方法有效
json.load(request.body)

