简体   繁体   English

Flask jsonify返回奇怪的数组?

[英]Flask jsonify returns weird array?

I'm trying to create a simple API using Flask. 我正在尝试使用Flask创建一个简单的API。 I now want to return a list of dicts as follows: 我现在想要返回一个dicts列表如下:

print results  # prints out [{'date': '2014-09-25 19:00:00', 'title': u'Some Title'}]
response = make_response(jsonify(results))
response.headers['Content-Type'] = 'application/json'
return response

But when I go the the url in the browser, I get the following: 但是当我在浏览器中访问网址时,我得到以下信息:

{
  "date": "title"
}

Does anybody know what I'm doing wrong here? 有谁知道我在这里做错了什么? All tips are welcome! 欢迎所有提示!

This is only an issue for Flask versions before 0.11 ; 对于0.11之前的 Flask版本这只是一个问题; if you still see this problem the best thing to do is to upgrade to a later version , as 0.10 is quite ancient by now. 如果你仍然看到这个问题,最好的办法是升级到更高版本 ,因为0.10现在已经非常古老了。


For Flask up to version 0.10, jsonify() will only accept dictionaries . 对于最高版本0.10的Flask, jsonify()只接受字典 If you give it a list, it'll turn the object into a dictionary, with dict(argument) . 如果你给它一个列表,它会把对象变成一个字典,用dict(argument) See the Flask.jsonify() documentation : 请参阅Flask.jsonify()文档

Creates a Response with the JSON representation of the given arguments with an application/json mimetype. 使用application/json mimetype创建具有给定参数的JSON表示的Response The arguments to this function are the same as to the dict constructor. 此函数的参数与dict构造函数的参数相同。

(Emphasis mine) (强调我的)

In your case you have a list with one element, and that element, when iterated over, has 2 values. 在您的情况下,您有一个包含一个元素的列表,并且该元素在迭代时具有2个值。 Those two values then become the key and value of the output dictionary: 这两个值然后成为输出字典的键和值:

>>> results = [{'date': '2014-09-25 19:00:00', 'title': u'Some Title'}]
>>> dict(results)
{'date': 'title'}

That's because the dict() constructor either takes another dictionary, keyword arguments or an iterable of (key, value) pairs. 那是因为dict()构造函数要么使用另一个字典,关键字参数, 要么使用(key, value)对的迭代。

The solution is to not pass in a list, but give it, at the very least, a key: 解决方案是不传入列表,但至少给它一个键:

response = jsonify(results=results)

jsonify() already returns a response object, there is no need to call make_response() on it. jsonify()已经返回一个响应对象,不需要在其上调用make_response() The above produces a JSON object with a 'results' key and your list as the value. 上面会生成一个带有'results'键的JSON对象,并将您的列表作为值。

jsonify() only takes a dictionary for security reasons. 出于安全原因, jsonify()只接受字典。 Quoting the documentation again: 再次引用文档:

For security reasons only objects are supported toplevel. 出于安全原因,仅支持对象。 For more information about this, have a look at JSON Security . 有关此内容的更多信息,请查看JSON安全性

If you really want to bypass this, you'll have to create your own response: 如果你真的想绕过这个,你必须创建自己的回复:

from Flask import json

response = make_response(json.dumps(results), mimetype='application/json')

This should no longer be an issue now that Flask's jsonify() method serializes top-level arrays (as of this commit ). 现在,Flask的jsonify()方法序列化顶级数组(截至此提交时 ),这应该不再是问题。

You'll need to be on Flask >= 0.11. 你需要在Flask上> = 0.11。

For convenience, you can either pass in a Python list: jsonify([1,2,3]) Or pass in a series of args : jsonify(1,2,3) 为方便起见,您可以传入Python列表: jsonify([1,2,3])或传入一系列argsjsonify(1,2,3)

Both will be converted to: [1,2,3] 两者都将转换为: [1,2,3]

Details here: http://flask.pocoo.org/docs/dev/api/#flask.json.jsonify 详情请访问: http//flask.pocoo.org/docs/dev/api/#flask.json.jsonify

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM