How to convert one depth json to several depth by a key in it?

Viewed 315

I got a JSON array from server,but it doesn't form right,like

 [
    {
      "id": 2,
      "parentId": 1,
    },
    {
      "id": 3,
      "parentId": 2,
    }
   ]

Absolutely,the 2nd item of the array should be a child item to the 1st because of its key "parentId",like

 [
    {
        "id": 2,
        "parentId": 1,
        "children": [
            {
                "id": 3,
                "parentId": 2,
            }
        ]
    }
 ]

How can I convert the 1st form of JSON to the 2nd?

1 Answers
Related