如何基于其成员的键来展平嵌套数组

时间:2014-03-14 17:31:48

标签: arrays json recursion nested flatten

我有以下JSON对象:

{
  "name": "Womens",
  "position": 1,
  "url": "/collections/womens-all",
  "submenus": [
    {
      "name": "Apparel",
      "position": 1,
      "url": "/collections/womens-apparel-all",
      "submenus": [
        {
          "name": "Tees & Tanks",
          "position": 1,
          "url": "/collections/womens-tees",
          "submenus": []
        },
        {
          "name": "Silk & Tops",
          "position": 2,
          "url": "/collections/womens-tops",
          "submenus": []
        },
        {
          "name": "Sweaters & Sweatshirts",
          "position": 3,
          "url": "/collections/womens-sweaters",
          "submenus": []
        }
      ]
    },
    {
      "name": "Accessories",
      "position": 2,
      "url": "/collections/womens-all-accessories",
      "submenus": [
        {
          "name": "Petra",
          "position": 1,
          "url": "/collections/petra",
          "submenus": []
        },
        {
          "name": "Weekenders",
          "position": 2,
          "url": "/collections/womens-bags",
          "submenus": []
        },
        {
          "name": "Backpacks & Totes",
          "position": 3,
          "url": "/collections/womens-backpacks",
          "submenus": []
        },
        {
          "name": "Accessories & Shoes",
          "position": 4,
          "url": "/collections/womens-accessories",
          "submenus": []
        }
      ]
    }
  ]
}

我想将此对象的子菜单展平为包含树中每个菜单的单个数组。结果如下:

[
  {
    "name": "Apparel",
    "position": 1,
    "url": "/collections/womens-apparel-all",
    "submenus": []
  },
  {
    "name": "Accessories",
    "position": 2,
    "url": "/collections/womens-all-accessories",
    "submenus": []
  },
  {
    "name": "Petra",
    "position": 1,
    "url": "/collections/petra",
    "submenus": []
  },
  {
    "name": "Weekenders",
    "position": 2,
    "url": "/collections/womens-bags",
    "submenus": []
  },
  {
    "name": "Backpacks & Totes",
    "position": 3,
    "url": "/collections/womens-backpacks",
    "submenus": []
  },
  {
    "name": "Accessories & Shoes",
    "position": 4,
    "url": "/collections/womens-accessories",
    "submenus": []
  },
  {
    "name": "Tees & Tanks",
    "position": 1,
    "url": "/collections/womens-tees",
    "submenus": []
  },
  {
    "name": "Silk & Tops",
    "position": 2,
    "url": "/collections/womens-tops",
    "submenus": []
  },
  {
    "name": "Sweaters & Sweatshirts",
    "position": 3,
    "url": "/collections/womens-sweaters",
    "submenus": []
  }
]

我觉得递归解决方案最好,但我无法做到。这个问题的最佳方法是什么?

1 个答案:

答案 0 :(得分:1)

这应该这样做,假设您在示例中的结果数组中忘记了“Womens”:

def flatten(dct, res=None):
    if res is None:
        res = []
    subdct = {}
    for k in dct:
        v = dct[k]
        if k == "submenus":
            for e in v:
                flatten(e, res)
            v = []
        subdct[k] = v
    res.append(subdct)
    return res

被称为

result = flatten(json_object)