简体   繁体   English

将 CSV 转换为 JSON,包括嵌套的对象数组

[英]Convert CSV to JSON including nested arrays of objects

In Excel I have created a spreadsheet that I would like to convert into JSON for my VS Code project.在 Excel 中,我创建了一个电子表格,我想将它转换为我的 VS Code 项目的 JSON。 I am currently just using an online CSV to JSON converter https://www.convertcsv.com/csv-to-json.htm , however my problem is that I can't seem to figure out a way to format it so that it uses arrays of objects.我目前只是使用在线 CSV 到 JSON 转换器https://www.convertcsv.com/csv-to-json.htm ,但是我的问题是我似乎无法找到一种方法来格式化它以便它使用对象数组。 eg例如

"arr": [
    {"id":1, "name": "obj1"},
    {"id":2, "name": "obj2"},
]

If I format in Excel like this:如果我像这样在 Excel 中格式化: 在此处输入图像描述

The output looks like this:输出如下所示:

[
 {
   "arr": {
      "id": [
         1,
         2
      ],
      "name": [
         "obj1",
         "obj2"
      ]
   }
}
]

Does anyone know how to format in Excel to get the desired array of objects?有谁知道如何在 Excel 中格式化以获得所需的对象数组? Otherwise can someone point me in the right direction for a script that will convert it correctly?否则有人可以为我指出正确的方向来正确转换它的脚本吗? Thanks!谢谢!

EDIT To add to the above, I should have been clearer.编辑要补充以上内容,我应该更清楚。 I understand that the initial rows in an Excel spreadsheet will convert to objects when parsed as JSON but what I am trying to achieve is converting to JSON with nested arrays.我知道 Excel 电子表格中的初始行在解析为 JSON 时将转换为对象,但我想要实现的是转换为带有嵌套数组的 JSON。 So for example my desired output would be:因此,例如我想要的输出是:

"arr":[
     {
       "id": 1, 
       "objects":[
          {"id": 1, "name": "obj1"}
          {"id": 2, "name": "obj2"}
       ]
     }
     {
       "id": 2, 
       "objects":[
          {"id": 1, "name": "obj1"}
       ]
     }
]

Given the following CSV data for example:例如,给定以下 CSV 数据:

  id,name
  1,foo
  2,bar

You can parse it as follow, where csv is the string containing CSV data:您可以按如下方式对其进行解析,其中csv是包含 CSV 数据的字符串:

  // split whole CSV into separated lines
  const lines = csv.split("\n");
  
  // split the first line to get properties count and names
  const keys = lines[0].split(",");
  
  const array = [];
  
  // iterate over the rest of lines, notice we start at 1
  for(let i = 1 ; i < lines.length; ++i) {
    
    // split line to get values
    const values = lines[i].split(",");
    
    // create new object
    const dict = {};
    
    // fill object with keys and values
    for(let k = 0; k < keys.length; ++k) {
      dict[keys[k]] = values[k];
    }
    
    // add object to array
    array.push(dict);
  }

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

相关问题 将 JSON 转换为 CSV 的最简单方法(包括数组到字段) - Easiest Way to Convert JSON to CSV (Including arrays to fields) 将字符串转换为嵌套对象和数组 - Convert string into nested objects and arrays 将数组转换为嵌套对象 - Convert arrays to nested objects javascript Javascript - 遍历 Json 对象并获取每个项目的分层键,包括嵌套对象和数组 - Javascript - Traverse through the Json Object and get each item hierarchical key including nested objects and arrays Javascript:将对象递归转换为字符串(包括子对象和数组),就好像在其周围有引号一样(与JSON.stringify不同) - Javascript: Convert Object to String Recursively (including child objects and arrays) as if there were quotes around it (unlike JSON.stringify) 将具有对象和数组的JSON转换为String - Convert JSON with objects and arrays to String JavaScript:将嵌套的 json 转换为 csv - JavaScript: Convert nested json to csv Javascript将数组中的嵌套对象转换为数组 - Javascript convert nested objects in array to arrays 将嵌套的对象数组转换为数组数组 - Convert nested Array of Objects into Array of Arrays 如何显示对象中的所有内容,包括嵌套对象和数组? - How to display everything from an object including nested objects and arrays?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM