递归转换json对象

时间:2016-11-12 15:32:37

标签: javascript arrays json recursion

我需要帮助递归地将json对象转换为另一个json对象。

我的输入对象如下所示:

var my_obj = {
  "id": 22,
  "eventTypeId": 37,
  "eventTypeName": "CommonOnly",
  "parentEvent": {
    "id": 21,
    "eventTypeId": 35,
    "eventTypeName": "FullTest",
    "parentEvent": {
      "id": 20,
      "parentEvent": null,
      "eventTypeId": 38,
      "eventTypeName": "FullTest"
    }
  },
  "childrenEvents": [
    {
      "id": 24,
      "parentEventId": 22,
      "eventTypeId": 36,
      "eventTypeName": "BlindedTest",
      "childrenEvents": []
    },
    {
      "id": 23,
      "parentEventId": 22,
      "eventTypeId": 38,
      "eventTypeName": "OneCustom",
      "childrenEvents": []
    }
  ]
}

我想要的输出是:

var my_obj = {
"id": 20,
"eventTypeId": 38,
"parentEvent": null,
"eventTypeName": "FullTest",
"childrenEvents": [{
    "id": 21,
    "eventTypeId": 35,
    "eventTypeName": "FullTest",
    "childrenEvents": [
        {
            "id": 22,
            "eventTypeId": 37,
            "eventTypeName": "CommonOnly",
            "childrenEvents": [
                {
                    "id": 24,
                    "parentEventId": 22,
                    "eventTypeId": 36,
                    "eventTypeName": "BlindedTest",
                    "childrenEvents": []
                }, {
                    "id": 23,
                    "parentEventId": 22,
                    "eventTypeId": 38,
                    "eventTypeName": "OneCustom",
                    "childrenEvents": []
                }
            ]
        }
    ]
}]

}

所以我希望能够得到一个对象,其中第二个parentEventparentEvent = null已成为根元素,数组为childrenEvents,子数组内的第一个父事件对象有自己的childrenEvents数组,在这个数组里面我想把现有的根对象放在自己的childrenEvents数组中

如果有人能帮我解决,我感激不尽。提前谢谢。

我的尝试

   function rebuild(input)
{
  var childrenEvents = [] ;

  for ( f in input.parentEvent )    // we may have attributes other than "age"
    if (f.parentEvent == null) {
        f.parentEvent.push(childrenEvents);
    } 
    else {
           f.parentEvent.push(input.parentEvent[f]);
    }

  if (input.childrenEvents.length !== 0)
    {
      input.parentEvent[f].push(input.childrenEvents)
    }

  return input;
}

    console.log(rebuild( $scope.myObj))

});

plunker

2 个答案:

答案 0 :(得分:3)

您可以使用对象构建所有依赖项,并仅使用

Dictionary<string, int> dictionary;
public void Form1_Load(object sender, EventArgs e)
{
    dictionary = new Dictionary<string, int>() { { "A", 1 }, { "B", 2 }, { "C", 3 } };
    dataGridView1.DataSource = dictionary.ToDataTable();
}
private void button1_Click(object sender, EventArgs e)
{
    dictionary.UpdateFromDataTable(dataGridView1.DataSource as DataTable);
}

as tree。

parentEvent === null
var my_obj = { "id": 22, "eventTypeId": 37, "eventTypeName": "CommonOnly", "parentEvent": { "id": 21, "eventTypeId": 35, "eventTypeName": "FullTest", "parentEvent": { "id": 20, "parentEvent": null, "eventTypeId": 38, "eventTypeName": "FullTest" } }, "childrenEvents": [{ "id": 24, "parentEventId": 22, "eventTypeId": 36, "eventTypeName": "BlindedTest", "childrenEvents": [] }, { "id": 23, "parentEventId": 22, "eventTypeId": 38, "eventTypeName": "OneCustom", "childrenEvents": [] }] },
    tree = function (data, root) {
        function iter(a) {
            var parent;
            if (a.childrenEvents && Array.isArray(a.childrenEvents)) {
                a.childrenEvents.forEach(iter);
            }
            a.childrenEvents = o[a.id] && o[a.id].childrenEvents;
            o[a.id] = a;
            if (a.parentEvent === root) {
                r = a;
            } else {
                if (a.parentEvent && 'id' in a.parentEvent) {
                    parent = a.parentEvent.id;
                }
                if ('parentEventId' in a) {
                    parent = a.parentEventId;
                }
                if (parent !== undefined) {
                    o[parent] = o[parent] || {};
                    o[parent].childrenEvents = o[parent].childrenEvents || [];
                    o[parent].childrenEvents.push(a);
                }
            }
            if (a.parentEvent) {
                iter(a.parentEvent);
                delete a.parentEvent;
            }
        }

        var r, o = {};
        iter(data);
        return r;
    }(my_obj, null);

console.log(tree);

答案 1 :(得分:-1)

你的问题的标题是“递归地”,但你的代码中没有递归(我可以看到)。哟需要从rebuild()内递归rebuild()递送。{/ p>

也许更接近这一点(没有运行它)......

function rebuild(input) {

    var rslt = input;  // If it stays this way, we've reached the end of the line

    var parent = inpt.parentEvent;
    if (parent) {
        parent.childrenEvents = [];
        parent.childrenEvents.push(input);
        inpt.parentEventId = parent.id;
        rslt = rebuild(parent);
    }

    return rslt;

}