简体   繁体   English

如何从 Python Flask API 返回特定格式的数据?

[英]How to return data in specific format from Python Flask API?

I am writing a get API using python flask.我正在使用 python flask 编写获取 API。 This API is for a FAQ webpage in which there are multiple question and answers which are divided section wise.此 API 用于一个常见问题解答网页,其中有多个按部分划分的问题和答案。

Webpage Example: How Webpage section looks for FAQ网页示例:网页部分如何查找常见问题解答

**Section 1**
    Question : Question 1 for section1?
    Answer : Answer 1 for section 1.
    
    Question : Question 2 for section1?
    Answer : Answer 2 for section1.

**Section 2**
    Question : Question 1 for section2?
    Answer : Answer 1 for section 1.
    
    Question : Question 2 for section2?
    Answer : Answer 2 for section1.

I have written this python API code我写了这个 python API 代码

@app.route('/getProductFaqs')
def productfaqs():
    try:
        conn = mysql.connect()
        cursor = conn.cursor(pymysql.cursors.DictCursor)
        cursor.execute("SELECT id, product_name, product_question, product_answer FROM questionFAQ")
        rows = cursor.fetchall()
        resp = jsonify(rows)
        resp.status_code = 200
        return resp
    except Exception as e:
        print(e)
    finally:
        cursor.close()
        conn.close()

which returns data in this format以这种格式返回数据

[
  {
    "id": 1,
    "product_answer": "answer product 1",
    "product_name": "product 1",
    "product_question": "What is product 1?"
  },
  {
    "id": 2,
    "product_answer": "answer product 2",
    "product_name": "product 2",
    "product_question": "What is product 2?"
  },
  {
    "id": 3,
    "product_answer": "answer product 3",
    "product_name": "product 3",
    "product_question": "What is product 3?"
  },
  {
    "id": 4,
    "product_answer": "answer product 4",
    "product_name": "product 4",
    "product_question": "What is product 4?"
  }
]

However my requirement for API response is in this format但是我对 API 响应的要求是这种格式

[
  {
    productid: 1[
      {
        Question: question1?
        Answer: answer1.
      },
      {
        Question: question2?
        Answer: Answer2.
      }
    ]
  },
 {
    productid: 2[
      {
        Question: question1?
        Answer: answer1.
      },
      {
        Question: question2?
        Answer: Answer2.
      }
    ]
  }
]

Can please someone help me out with this formatting of response.请有人帮我解决这种格式的回复。 Thankyou in Advance.先感谢您。

my friend.我的朋友。 I have some tips for you.我有一些建议给你。 First of all, you should not use a database connection like this.首先,您不应该使用这样的数据库连接。 This way, you can not easily use it in other parts of your program.这样,您就不能在程序的其他部分轻松使用它。 Second, It's usually better to use an ORM like Sqlalchemy integrated with Flask.其次,通常最好使用 ORM 之类的 Sqlalchemy 与 Flask 集成。 It will help you satisfy also the first problem.它还将帮助您满足第一个问题。

And for your question, you should parse the result of your query, tailor it to fulfill your requirements, and store it in a dictionary so that you can then return it as a JSON object.对于您的问题,您应该解析查询结果,对其进行定制以满足您的要求,并将其存储在字典中,以便您可以将其作为 JSON object 返回。

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

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