javascript从数组创建嵌套的JSON数组

时间:2018-05-05 03:41:37

标签: javascript arrays json nested

我有这个数组:
myArray = [“Id = 1”,“Time = 18:40”,“Topic = yyyyyyyyyyyyy”,“GUEST”,“Role = HS”,“Infos = Arizona”,“Role = GS”,“Infos = Arizona” ,“Role = GS”,“Infos = Colorado”,“Id = 2”,“Time = 11:32”,“Topic = xoxo”,“GUEST”,“Role”=“GS”,“Infos = California” ,“角色= CS”,“信息=马里兰”,“角色= GS”,“信息=内华达”];

我想从中创建一个嵌套的JSON数组:

myJson = [
{  
 "Id" : "1",
 "Time" : "18:40",
 "Topic" : "yyyyyyyyyyyy",
 "GUEST":
 [
   {"Role" : "HS",
   "Infos" : "Arizona"},
   {"Role" : "GS",
   "Infos" : "Arizona"},
   {"Role" : "HS",
   "Infos" : "Colorado"}
 ]
},
{  
 "Id" : "2",
 "Time" : "11:32",
 "Topic" : "xoxo",
 "GUEST":
 [
   {"Role" : "GS",
   "Infos" : "California"},
   {"Role" : "CS",
   "Infos" : "Maryland"},
   {"Role" : "GS",
   "Infos" : "Nevada"}
 ]
}
]  

我该怎么办?尝试下面的代码没有成功。

myArray = ["Id = 1","Time = 18:40","Topic = yyyyyyyyyyyy","GUEST","Role = HS","Infos = Arizona","Role = GS","Infos = Arizona","Role = GS","Infos = Colorado","Id = 2","Time = 11:32","Topic = xoxo","GUEST","Role" = "GS","Infos = California","Role = CS","Infos = Maryland","Role = GS","Infos = Nevada"];

// Declaring new object
			let obj = {};
			// Place to remember current level of object
			let level;
			myJson = [];
			for (let item of this.myArray) {
			  // If it contains an equals
			  if (item.includes('=')) {
			    // Split it into two stings
			    let split = item.split('=');
			    let key = split[0].trim();
			    let val = split[1].trim();
			    // If we're already on a lower level like GUEST3 put it in there
			    if (level) {
			      obj[level][key] = val
			    } else {
			      // Or just place the new data at the top level
			      obj[key] = val
			    }
			  } else {
			    // If there's no equals we want to go down a layer
			    level = item;
			    myJson.push(obj[item]);
			  }
			}

console.log("myJson : "+ JSON.stringify(myJson));

2 个答案:

答案 0 :(得分:1)

另一个答案是完全正确的 - 您要求的格式是完全错误的。 如果当你遇到“GUEST”字符串并且当你遇到下一个"Id = \d+"字符串时在输出中创建新对象时,它只会更深一个对象,你可以使用类似的东西: p>

  let process = (arr) => {
    let path = [];
    const data = [];
    let o = data;
    while (arr && arr.length) {
      let item = arr.shift();
      let key= null;
      let value = null;

      if (/Id = \d+/.test(item)) {
        o = {};
        data.push(o);
        let pair = item.split(' = ');
        o.Id = pair[1];      
      } 
      else if (item == "GUEST") {
        o["GUEST"] = {};
        o = o["GUEST"]      
        value = {};      
      } else {
        let pair = item.split(' = ');
        o[pair[0]] = pair[1];       
      }      
    }  
    return data;
  }

然而,使用这种方法,您的重复键将被覆盖,如下所示:

 [
    {
      "Id": "1",
      "Time": "18:40",
      "Topic": "yyyyyyyyyyyy",
      "GUEST": {
        "Role": "GS",
        "Infos": "Colorado"
      }
    },
    {
      "Id": "2",
      "Time": "11:32",
      "Topic": "xoxo",
      "GUEST": {
        "Role": "GS",
        "Infos": "Nevada"
      }
    }
  ]

我认为这是一个好的开始,你可以根据自己的喜好微调解析GUEST部分

答案 1 :(得分:0)

JSON的格式错误。

  1. JSON中不允许使用键值对数组。
  2. 键值对数组不能具有相同的名称键。
  3. 更改为以下格式(JSON对格式有严格要求):

    myJson = [
    {  
     "Id" : "1",
     "Time" : "18:40",
     "Topic" : "yyyyyyyyyyyy",
     "GUEST":
     [
       {
         "Role" : "HS",
         "Infos" : "Arizona"
       },
       {
         "Role" : "GS",
         "Infos" : "Arizona"
       },
       {
         "Role" : "HS",
        "Infos" : "Colorado"
       }
     ]
    }
    ]