1

我正在使用一个 RDBMS,其中包含这样存储的分层对象列表:

Id    Name                  ParentId
====================================
1     Food                  NULL
2     Drink                 NULL
3     Vegetables            1
4     Fruit                 1
5     Liquor                2
6     Carrots               3
7     Onions                3
8     Strawberries          4
...
999   Celery                3

我不知道选择它的具体原因,但它是固定的,因为系统的其余部分依赖于以这种形式获取结构。

我想使用 RESTful API 通过JSON公开这些数据,并且我希望以以下格式(数组数组)输出它:

item:
{
     id: 1, Description: "Food",
     items: [
         {
             id: 3, Description: "Vegetables",
             items: [ ... ]
         },
         {
             id: 4, Description: "Fruit",
             items: [ ... ]
         }
     ]
},
item:
{
    id: 2, Description: "Drink",
    items: [ ... ]
}

什么是循环数据并产生所需输出的明智方法?我正在使用C#进行开发,但如果有其他语言的库或示例,我很乐意在可能的情况下重新实现。

谢谢 :)

4

0 回答 0