Flask 使用 simplejson 来实现JSON。自从 simplejson 既在标准库中提供也在 Flask 的拓展中提供。Flask将首先尝试自带的simplejson,如果失败了就使用标准库中的json模块。除此之外,为了更容易定制它还会委托访问当前应用的JSON的编码器和解码器。
你可以这样用
For usage examples, read the json documentation. 关于更多的用法,请阅读标准库中的 json 文档。下面的拓展已经默认被集成到了标准库中JSON模块里:
这个 htmlsafe_dumps() 方法也能在 Jinja2 的过滤器中使用,名字为 |tojson 。请注意在 script 标签内部的内容将不会被转义,所以如果你想在 script 内部使用的话请确保它是不可用的通过 |safe 来转义,除非你正在使用 Flask 0.10,如下:
-
flask.json.
jsonify
(
*args,
**kwargs
)
-
Creates a Response with the JSON representation of the given arguments with an application/json mimetype. The arguments to this function are the same as to the dict constructor.
Example usage:
This will send a JSON response like this to the browser:
For security reasons only objects are supported toplevel. For more information about this, have a look at JSON 安全.
This function’s response will be pretty printed if it was not requested with X-Requested-With: XMLHttpRequest to simplify debugging unless the JSONIFY_PRETTYPRINT_REGULAR config parameter is set to false.
-
- class werkzeug.wrappers. BaseResponse ( response=None, status=None, headers=None, mimetype=None, content_type=None, direct_passthrough=False )
-
Base response class. The most important fact about a response object is that it’s a regular WSGI application. It’s initialized with a couple of response parameters (headers, body, status code etc.) and will start a valid WSGI response when called with the environ and start response callable.
Because it’s a WSGI application itself processing usually ends before the actual response is sent to the server. This helps debugging systems because they can catch all the exceptions before responses are started.
Here a small example WSGI application that takes advantage of the response objects:
Like BaseRequest which object is lacking a lot of functionality implemented in mixins. This gives you a better control about the actual API of your response objects, so you can create subclasses and add custom functionality. A full featured response object is available as Response which implements a couple of useful mixins.
To enforce a new type of already existing responses you can use theforce_type() method. This is useful if you’re working with different subclasses of response objects and you want to post process them with a know interface.
Per default the request object will assume all the text data is utf-8encoded. Please refer to the unicode chapter for more details about customizing the behavior.
Response can be any kind of iterable or string. If it’s a string it’s considered being an iterable with one item which is the string passed. Headers can be a list of tuples or a Headers object.
Special note for mimetype and content_type: For most mime typesmimetype and content_type work the same, the difference affects only ‘text’ mimetypes. If the mimetype passed with mimetype is a mimetype starting with text/, the charset parameter of the response object is appended to it. In contrast the content_type parameter is always added as header unmodified.