如何反转JSON对象的层次结构?

时间:2017-01-31 19:04:45

标签: javascript json object

我如何使用javascript“从里面翻出”从服务器API收到的JSON对象?

示例输入

[
    {
        "id": 7,
        "idAsignacion": 9,
        "idPregunta": 4,
        "cumplimiento": 1,
        "observacionNumeral": 20,
        "observacionEscrita": "HOLAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
        "rutaObservacionDocumental": "1/1/1/1/4",
        "created_at": "2017-01-31 18:26:44",
        "updated_at": "2017-01-31 18:26:44",
        "traer_preguntas": {
            "id": 4,
            "idRequisito": 1,
            "ordenPreguntas": 1,
            "pregunta": "jojgpofdkñkñdkgñk",
            "tecnicaAuditoria": "Observación",
            "escrita": 1,
            "numeral": 1,
            "documental": 1,
            "estado": 0,
            "created_at": "2017-01-31 15:42:41",
            "updated_at": "2017-01-31 15:42:41",
            "obtener_requisitos": {
                "id": 1,
                "ordenRequisito": 1,
                "idDimension": 1,
                "nombreRequisito": "Requisito uno",
                "estado": 0,
                "created_at": "2017-01-30 15:19:02",
                "updated_at": "2017-01-30 15:19:02",
                "obtener_dimensiones": {
                    "id": 1,
                    "ordenDimension": 1,
                    "dimension": "Dimension UNO",
                    "estado": 0,
                    "created_at": "2017-01-30 15:18:48",
                    "updated_at": "2017-01-30 15:18:48"
                }
            }
        }
    }
]

期望的输出

"obtener_dimensiones": {
    "id": 1,
    "ordenDimension": 1,
    "dimension": "Dimension UNO",
    "estado": 0,
    "created_at": "2017-01-30 15:18:48",
    "updated_at": "2017-01-30 15:18:48""obtener_requisitos": {
        "id": 1,
        "ordenRequisito": 1,
        "idDimension": 1,
        "nombreRequisito": "Requisito uno",
        "estado": 0,
        "created_at": "2017-01-30 15:19:02",
        "updated_at": "2017-01-30 15:19:02",
        "traer_preguntas": {
            "id": 4,
            "idRequisito": 1,
            "ordenPreguntas": 1,
            "pregunta": "jojgpofdkñkñdkgñk",
            "tecnicaAuditoria": "Observación",
            "escrita": 1,
            "numeral": 1,
            "documental": 1,
            "estado": 0,
            "created_at": "2017-01-31 15:42:41",
            "updated_at": "2017-01-31 15:42:41",
            {
                "id": 7,
                "idAsignacion": 9,
                "idPregunta": 4,
                "cumplimiento": 1,
                "observacionNumeral": 20,
                "observacionEscrita": "HOLAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
                "rutaObservacionDocumental": "1/1/1/1/4",
                "created_at": "2017-01-31 18:26:44",
                "updated_at": "2017-01-31 18:26:44",

            }
        }
    }
}

如何将最后一个元素变成第一个元素,依此类推,直到每个“child”属性成为它的原始父元素的父元素?

1 个答案:

答案 0 :(得分:3)

输入:

obj = [{
    "id": 7,
    "idAsignacion": 9,
    "idPregunta": 4,
    "cumplimiento": 1,
    "observacionNumeral": 20,
    "observacionEscrita": "HOLAAAAAAAAAAAAAAAAAAAA",
    "rutaObservacionDocumental": "1/1/1/1/4",
    "created_at": "2017-01-31 18:26:44",
    "updated_at": "2017-01-31 18:26:44",
    "traer_preguntas": {
        "id": 4,
        "idRequisito": 1,
        "ordenPreguntas": 1,
        "pregunta": "jojgpofdkñkñdkgñk",
        "tecnicaAuditoria": "Observación",
        "escrita": 1,
        "numeral": 1,
        "documental": 1,
        "estado": 0,
        "created_at": "2017-01-31 15:42:41",
        "updated_at": "2017-01-31 15:42:41",
        "obtener_requisitos": {
            "id": 1,
            "ordenRequisito": 1,
            "idDimension": 1,
            "nombreRequisito": "Requisito uno",
            "estado": 0,
            "created_at": "2017-01-30 15:19:02",
            "updated_at": "2017-01-30 15:19:02",
            "obtener_dimensiones": {
                "id": 1,
                "ordenDimension": 1,
                "dimension": "Dimension UNO",
                "estado": 0,
                "created_at": "2017-01-30 15:18:48",
                "updated_at": "2017-01-30 15:18:48"
            }
        }
    }
}];

守则:

rd(obj[0])
newObj = {}
kn1 = Object.keys(th[2])
newObj[kn1] = th[2][kn1]
kn2 = Object.keys(th[1])
newObj[kn1][kn2] = th[1][kn2]
kn3 = Object.keys(th[0])
newObj[kn1][kn2][kn3] = th[0][kn3]
newObj[kn1][kn2][kn3].root = thr
console.log(JSON.stringify([newObj], null, 4));

function rd(mo, od) {
    if (typeof(od) === "undefined") {th = {}; od = 0}
    else {od++}
    Object.keys(mo).forEach(function(k) {
        if (typeof(mo[k]) === "object") {
            console.group("depth=" + od + " : " + k + "(" + Object.keys(mo[k]).length + ")");
            rd(mo[k], od)
            console.groupEnd();
            if (typeof(th[od]) === "undefined") th[od] = {}
            th[od][k] = mo[k]
            Object.keys(th[od][k]).forEach(function(ik) {
              if (typeof(th[od][k][ik]) === "object") {
                delete th[od][k][ik]
              }
            })
        } else {
            console.log("depth=" + od + " : " + k)
            if (od === 0) {
                if (typeof(thr) === "undefined") thr = {}
                thr[k] = mo[k]
            }
        }
    });
}

代码的第二部分可以大大改进,如果需要我可以继续工作。 我添加了Key Name root以使JSON有效

输出:

[
    {
        "obtener_dimensiones": {
            "id": 1,
            "ordenDimension": 1,
            "dimension": "Dimension UNO",
            "estado": 0,
            "created_at": "2017-01-30 15:18:48",
            "updated_at": "2017-01-30 15:18:48",
            "obtener_requisitos": {
                "id": 1,
                "ordenRequisito": 1,
                "idDimension": 1,
                "nombreRequisito": "Requisito uno",
                "estado": 0,
                "created_at": "2017-01-30 15:19:02",
                "updated_at": "2017-01-30 15:19:02",
                "traer_preguntas": {
                    "id": 4,
                    "idRequisito": 1,
                    "ordenPreguntas": 1,
                    "pregunta": "jojgpofdkñkñdkgñk",
                    "tecnicaAuditoria": "Observación",
                    "escrita": 1,
                    "numeral": 1,
                    "documental": 1,
                    "estado": 0,
                    "created_at": "2017-01-31 15:42:41",
                    "updated_at": "2017-01-31 15:42:41",
                    "root": {
                        "id": 7,
                        "idAsignacion": 9,
                        "idPregunta": 4,
                        "cumplimiento": 1,
                        "observacionNumeral": 20,
                        "observacionEscrita": "HOLAAAAAAAAAAAAAAAAAAAA",
                        "rutaObservacionDocumental": "1/1/1/1/4",
                        "created_at": "2017-01-31 18:26:44",
                        "updated_at": "2017-01-31 18:26:44"
                    }
                }
            }
        }
    }
]