如何在Bottle中处理JSON请求?

时间:2022-10-24 13:42:32

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.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.

所以问题是我无法通过在dict中转换request.GET来正确处理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?

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

2 个解决方案

#1


22  

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

现在自瓶子0.10起,request.json就可以使用:)

Document is here.

文件在这里。

#2


18  

request.json is 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)

#1


22  

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

现在自瓶子0.10起,request.json就可以使用:)

Document is here.

文件在这里。

#2


18  

request.json is 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)