JSON解析变量结果

时间:2019-03-15 09:01:12

标签: javascript json parsing output

我有一个用xhr查询的JSON。这些对象(人)包含一个我必须拆分的键-值对,称为“ serviceLevel”。

当我将其堆叠在变量(服务)中并像这样记录时:

let main = document.getElementsByTagName('main');

getXHR("GET", './db/orga.json', (success) => {
    format(success)
}, (error) => {
    console.error(error)
});

function format() {
    let people = arguments[0];
    for (let i in people) {
        let person = people[i];
        let services = person.serviceLevel.split(".");
        console.log(services);
		console.log(person.serviceLevel.split("."));

        let idCard = document.createElement('div');
        idCard.id = person.firstName + person.familyName;
        idCard.classList.add('person');
        idCard.innerHTML = "<div class=\"item nom\"><span class=\"prenom\">" + person.firstName + "</span><span class=\"famille\">" + person.familyName + "</span></div>";
        idCard.innerHTML += "<span class=\"job\">" + person.jobTitle_1 + "</span>";
        idCard.innerHTML += "<span class=\"mail\"><a href=\"mailto:" + person.mail + "\">" + person.mail + "</span>";
        idCard.innerHTML += "<span class=\"tel\"><a href=\"tel:" + person.phone_1 + "\">" + person.phone_1 + "</span>";
        idCard.innerHTML += "<span class=\"tel\"><a href=\"tel:" + person.mobile + "\">" + person.mobile + "</span>";

        for (let j in services) {
            let serviceElement = document.getElementById(services[j]);
            if (!serviceElement) {
                let serviceElement = document.createElement('div');
                serviceElement.id = services[j];
                serviceElement.classList.add('n' + j, "service");
                serviceElement.innerHTML = "<span class=\"title\">" + services[j] + "</span>";

                if (j == 0) {
                    if (services[services.length - 1] = j) {
                        serviceElement.appendChild(idCard);
	                    main[0].appendChild(serviceElement);
                    }
                } else {
                    let parent = services[j - 1],
                        parentService = document.getElementById(parent);
                    if (services[services.length - 1] = j) {
                        serviceElement.appendChild(idCard);
                    }
                    parentService.appendChild(serviceElement);
                }
            } else {
                serviceElement.appendChild(idCard);
            }
        }
    }
}
const data = [{
  "Tri": "blablablabla, CSMSI.SAFS, n, XXXX, YYYY",
  "Department": "The best department",
  "serviceLevel": "CSMSI.SAFS",
  "organisationLevel": "blablablabla",
  "rang": "n",
  "familyName": "XXXX",
  "firstName": "YYYY",
  "jobTitle_2": "Directeur",
  "jobTitle_1": "Directeur",
  "phone_1": "nn nn nn nn nn",
  "phone_2": "",
  "mobile": "nn nn nn nn nn",
  "mail": "xxxx.yyyy@zzzz.fr",
  "location": "france"
}];

format(data);

结果不同:

(2) ["CSMSI", "SAFS"]
0: "CSMSI"
1: "SAFS"
length: 2

(2) ["CSMSI", "SAFS"]
0: "CSMSI"
1: "1"
length: 2

JavaScript console screenshot

我们可以看到,“服务”的内容不错,但是当我扩展树时,第二个键值的值为“ 1”……这是一个问题。有办法改变吗?

1 个答案:

答案 0 :(得分:0)

当我在for循环中使用“经典”(i = 0; i