瓶子以简洁的方式显示Json [英] Flask Display Json in a Neat Way

查看:125
本文介绍了瓶子以简洁的方式显示Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b

  app.route(' (主要):
course_list = list(client.db.course_col.find({major:(major.encode(utf8,ignore ).upper())}))
return json.dumps(course_list,sort_keys = True,indent = 4,default = json_util.default)

当用CSCI主要回报时:


[{course :CSCI052,description:计算机科学基础,在函数式编程,程序和数据抽象,递归和问题解决方面奠定了坚实的基础。 ,程序设计语言,有限自动机和可计算性,本课程与HM 60一样,是Claremon任何一门计算机科学课程的前提条件t学院。先决条件:51.,教师:Bull,Everett L.,Jr.,名字:计算机科学基础,数字:52,学校:PO}] p>

如何返回这个字典,以便每个键和值都在他们自己的行上?



<$>
(主要):
course_list = list(client.db.course_col.find({/< major> /')
def major_res major:major.encode('utf8','ignore')。upper()}))
return flask.jsonify(** course_list)

这将返回 jsonify 的kwargs作为格式良好的json响应,与您的代码不同,正确 Content-Type 标题: application / json

然而,请注意文档对此的评论:


这个函数的响应将被打印出来如果没有请求 X-Requested-With:XMLHttpRequest 来简化调试,除非JSONIFY_PRETTYPRINT_REGULAR配置参数设置为false




这意味着ajax响应总是会收到非美化的JSON(因为Same Origin策略使得不可能取消设置 X-Requested-With )。但我想这不会是一个很大的问题,因为JSON消费不应该被格式化(这只是额外的数据通过电线发送)。如果你想继续使用 json.dumps()(非漂亮的打印),你可以使用通过返回一个 flask.Response ,但是,您应该考虑这个安全含义

  app.route('/< major> /')
def major_res ):
course_list = list(client.db.course_col.find({major:major.encode('utf8','ignore')。upper()})
return flask.Response (response = json.dumps(course_list),status = 200,mimetype ='application / json')

有关更多的差异:




I'm creating an API from a mongodb database using Flask and have the following code:

app.route('/<major>/')
def major_res(major):
     course_list  = list(client.db.course_col.find( {"major" : (major.encode("utf8",       "ignore").upper())}))
   return json.dumps(course_list, sort_keys=True, indent=4, default=json_util.default)

Which when called with the CSCI major returns:

[ { "course": "CSCI052", "description": "Fundamentals of Computer Science. A solid foundation in functional programming, procedural and data abstraction, recursion and problem-solving. Applications to key areas of computer science, including algorithms and complexity, computer architecture and organization, programming languages, finite automata and computability. This course serves the same role as HM 60 as a prerequisite for upper-division computer science courses at any of the Claremont Colleges. Prerequisite: 51.", "instructor": "Bull, Everett L.,, Jr.", "name": " Fundamentals of Computer Science", "number": 52, "school": "PO" }]

How do I return this dictionary so that each key and value are on their own line?

解决方案

Flask provides jsonify() as a convenience:

app.route('/<major>/')
def major_res(major):
    course_list = list(client.db.course_col.find({"major": major.encode('utf8', 'ignore').upper() }))
    return flask.jsonify(**course_list)

This will return the kwargs of jsonify as a nicely formatted json response and, unlike your code, will send the proper Content-Type header: application/json.

However, take note of what the docs say about this:

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.

This means that ajax responses will always receive non-prettyprinted JSON (since the Same Origin policy makes it impossible to unset X-Requested-With). But I imagine this won't be much of a problem since JSON for JS consumption shouldn't need to be formatted (that's just extra data to be sent over the wire).

If you'd like to still use json.dumps() (non-pretty printed), you can send the proper mimetype by returning a flask.Response, however, you should consider the security implications of this:

app.route('/<major>/')
def major_res(major):
    course_list = list(client.db.course_col.find({"major": major.encode('utf8', 'ignore').upper() }))
    return flask.Response(response=json.dumps(course_list), status=200, mimetype='application/json')

For more on the difference:

这篇关于瓶子以简洁的方式显示Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆