简体   繁体   English

将数组转换为嵌套的 JSON

[英]Convert an Array To a Nested JSON

I am struggling the last days with an unresolved problem for me.我在最后几天为一个未解决的问题而苦苦挣扎。 I am stuck on this problem for long time that's why I took the decision to ask for your help.我被这个问题困扰了很长时间,这就是为什么我决定寻求您的帮助。

I'm reading an Excel file and got the following array which I am now trying to convert it to a nested JSON to get accepted from the mongoDB but unfortunately I have difficulties to get there.我正在读取一个 Excel 文件并获得以下数组,我现在正尝试将其转换为嵌套的 JSON 以从 mongoDB 中获得接受,但不幸的是我很难到达那里。

var arr = ["number,isnewLanguage,label,numberOfChoices,languages/language,questions/mainText/language,questions/subText/language,questions/choices/language,questions/fields/langugage",
  "1,false,label1,5,English,MAinText1,SubText1,choices1,false",
  "2,false,label2,5,English,MAinText2,SubText2,choices2,false",
  "3,true,label1,false,Italian,MainTextItalian1,SubTextItalian1,choicesItalian1,false",
  "4,true,label2,false,Italian,MAinTextItalian2,SubTextItalian2,choicesItalian2,false"
]

I would like to end up with the following structure.我想最终得到以下结构。 Here is a fiddle with my work https://jsfiddle.net/argusDob/3py6fd8m/2/这是我的工作https://jsfiddle.net/argusDob/3py6fd8m/2/

var json = [{
    number: 1,
    label1: "label1",
    numberOfChoices: 5,
    languages: [{
        language: "English",
        questions: [{
          mainText: "MainText1",
          subText: "subText1",
          choices: "choices1",
          fields: "false"
        }]
      },
      {
        language: "Italian",
        questions: [{
          mainText: "MainTextItalian1",
          subText: "subTextItlaian1",
          choices: "choicesItalian1",
          fields: "false"
        }]
      }
    ]
  },
  {
    number: 2,
    label1: "label2",
    numberOfChoices: 5,
    languages: [{
        language: "English",
        questions: [{
          mainText: "MainText2",
          subText: "subText2",
          choices: "choices2",
          fields: "false"
        }]
      },
      {
        language: "Italian",
        questions: [{
          mainText: "MainTextItalian2",
          subText: "subTextItlaian2",
          choices: "choicesItalian2",
          fields: "false"
        }]
      }
    ]
  }
]

 var arr = ["number,isnewLanguage,label,numberOfChoices,languages/language,questions/mainText/language,questions/subText/language,questions/choices/language,questions/fields/langugage", "1,false,label1,5,English,MAinText1,SubText1,choices1,false", "2,false,label2,5,English,MAinText2,SubText2,choices2,false", "3,true,label1,false,Italian,MainTextItalian1,SubTextItalian1,choicesItalian1,false", "4,true,label2,false,Italian,MAinTextItalian2,SubTextItalian2,choicesItalian2,false" ] var attrs = arr.splice(0, 1); var test = []; var result = arr.map(function(row, idx) { var myObj = {}; var array = []; var therows = row.split(",").slice(1); console.log(therows[0]); therows.forEach(function(value, i) { var myKeys = attrs[0].split(",").slice(1) var theKeys = myKeys[i] if (theKeys.split('/').length === 1) { myObj[theKeys] = value; } //languages var theLanguagesKey = theKeys.split("/")[0] if (theKeys.split("/").length == 2) { if (!myObj["languages"]) { var theNewLanguage = {} myObj["languages"] = []; theNewLanguage[theKeys.split('/')[1]] = value; myObj["languages"].push(theNewLanguage); } } //questions if (theKeys.split("/").length == 3) { if (!myObj["questions"]) { var theNewQuestions = {} myObj["questions"] = []; theNewQuestions[theKeys.split('/')[1]] = value; myObj["questions"].push(theNewQuestions); } else { for (var i = 0; i < myObj["questions"].length; i++) { myObj["questions"][i][theKeys.split('/')[1]] = value; } } } }) test.push(myObj) }) console.log(test)

If you don't want to reinvent the wheel, check fast-csv .如果您不想重新发明轮子,请检查fast-csv It has pretty good options for what you need.它为您提供了非常好的选择。 If your headers are at the first row, you can pass headers to true to convert the object that can accept by mongodb.如果您的标题在第一行,您可以将标题传递给 true 以转换 mongodb 可以接受的对象。 Here is the example 这是例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM