首页 > 解决方案 > 如何在 FLASK 中创建分层 json 响应

问题描述

我在数据库中有一个表,例如数据库表。我想从数据库中搜索一个孩子并将分层 JSON 返回到前端以创建一棵树。我怎么能在烧瓶中做到这一点。我对 mat 的预期 JSON 应该像预期的 JSON

标签: jsonsqliteflaskflask-sqlalchemyhierarchical-data

解决方案


标记您的问题,因此本文假设您也在使用 Python。要将数据库值格式化为 JSON 字符串,您可以查询 db,然后使用递归:

import sqlite3, collections
d = list(sqlite3.connect('file.db').cursor().execute("select * from values"))
def get_tree(vals):
  _d = collections.defaultdict(list)
  for a, *b in vals:
    _d[a].append(b)
  return [{'name':a, **({} if not (c:=list(filter(None, b))) else {'children':get_tree(b)})} for a, b in _d.items()]

import json
print(json.dumps(get_tree(d), indent=4))

输出:

[
  {
    "name": "AA",
    "children": [
        {
            "name": "BB",
            "children": [
                {
                    "name": "EE",
                    "children": [
                        {
                            "name": "JJ",
                            "children": [
                                {
                                    "name": "EEV"
                                },
                                {
                                    "name": "FFW"
                                }
                            ]
                        },
                        {
                            "name": "KK",
                            "children": [
                                {
                                    "name": "HHX"
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        {
            "name": "CC",
            "children": [
                {
                    "name": "FF",
                    "children": [
                        {
                            "name": "LL",
                            "children": [
                                {
                                    "name": "QQY"
                                }
                            ]
                        },
                        {
                            "name": "MM",
                            "children": [
                                {
                                    "name": "RRV"
                                }
                            ]
                        }
                    ]
                },
                {
                    "name": "GG",
                    "children": [
                        {
                            "name": "NN",
                            "children": [
                                {
                                    "name": "SSW"
                                }
                            ]
                        }
                    ]
                }
            ]
        },
        {
            "name": "DD",
            "children": [
                {
                    "name": "HH",
                    "children": [
                        {
                            "name": "OO",
                            "children": [
                                {
                                    "name": "TTZ"
                                }
                            ]
                        }
                    ]
                },
                {
                    "name": "II",
                    "children": [
                        {
                            "name": "PP",
                            "children": [
                                {
                                    "name": "UUW"
                                }
                             ]
                         }
                      ]
                  }
              ]
          }
      ]
   }
]

推荐阅读