javascript - 将 CSV 转换为 JSON (JS) 时如何替换字符串中的逗号

标签 javascript jquery arrays json csv

我在尝试使用 JS 将客户端提供的 CSV 转换为 JSON 数据时遇到映射问题。 CSV 中的一列包含将包含逗号的地址数据。我试过更改定界符,但出于某种原因,当我阅读 CSV 时,JS 忽略了设置的定界符并将它们转换为逗号。这意味着将数据转换为 JSON 时的映射不正确。代码和输出如下:-

JS:-

    $(document).ready(function() {
    $.ajax({
        type: "GET",
        url: "result.csv",
        dataType: "text",
        success: function(data) { $("body").append(csvJSON(data));}
     });
});

function csvJSON(csv){

    var lines=csv.split("\n");

    var result = [];

    var headers=lines[0].split(",");

    for(var i=1;i<lines.length;i++){

        var obj = {};
        var currentline=lines[i].split(",");


        for(var j=0;j<headers.length;j++){

            obj[headers[j]] = currentline[j];
        }

        result.push(obj);

    }

    return JSON.stringify(result); 
  }

CSV 数据:-

"type","geometry__type","geometry__coordinates__001","geometry__coordinates__002","properties__name","properties__address","properties__address2","properties__city","properties__state","properties__postal","properties__country","properties__phone","properties__phoneFormatted","properties__email","properties__web"
"Stockist","Point",-110.788,43.4705,"Whitechapel Ltd","Box 11719, 1135 Maple Way","Jackson,","Wyoming","WY",83002,"US",13077399478,"+1 307 739-9478","whitechapel@wyoming.com","www.whitechapel-ltd.com"
"Stockist","Point",103.82705,1.30637,"Thrive Design & Trading","19, Tanglin Road, #03-35","Tanglin Shopping Centre","Singapore",,247909,"Singapore","65-67357333","65-67357333","francis@thrive-products.com.sg",

当前结果:-

   {  
      "type":"Stockist",
      "geometry__type":"Point",
      "geometry__coordinates__001":"-110.788",
      "geometry__coordinates__002":"43.4705",
      "properties__name":"Whitechapel Ltd",
      "properties__address":"\"Box 11719",
      "properties__address2":" 1135 Maple Way\"",
      "properties__city":"\"Jackson",
      "properties__state":"\"",
      "properties__postal":"Wyoming",
      "properties__country":"WY",
      "properties__phone":"83002",
      "properties__phoneFormatted":"US",
      "properties__email":"13077399478",
      "properties__web\r":"+1 307 739-9478"
   },

期望的结果:-

  {  
      "type":"Stockist",
      "geometry__type":"Point",
      "geometry__coordinates__001":"-110.788",
      "geometry__coordinates__002":"43.4705",
      "properties__name":"Whitechapel Ltd",
      "properties__address":"Box 11719, 1135 Maple Way",
      "properties__address2":"Jackson,",
      "properties__city":"Wyoming",
      "properties__state":"WY",
      "properties__postal":"83002",
      "properties__country":"US",
      "properties__phone":"13077399478",
      "properties__phoneFormatted":"+1 307 739-9478",
      "properties__email":"whitechapel@wyoming.com",
      "properties__web":"www.whitechapel-ltd.com"
   },

最佳答案

因为你所有的值都用引号括起来,你应该将你的拆分参数更改为一个正则表达式 only splits when a comma is outside of a pair of quotes .

由于数据的格式化方式,您应该记住,您将需要清理大量不必要的转义引号字符串。但是您必须确保您不会不小心清除 csv 中已经转义的任何引号。但我想这些边缘情况是人们使用预构建库的原因。

编辑

关于我之前的声明。您可能只需删除字符串开头和结尾的引号即可。但我只是要抛出一个免责声明,并说你对这个结果的理解可能会因你的数据而异。

const csv=`\"type","geometry__type","geometry__coordinates__001","geometry__coordinates__002","properties__name","properties__address","properties__address2","properties__city","properties__state","properties__postal","properties__country","properties__phone","properties__phoneFormatted","properties__email","properties__web"
"Stockist","Point",-110.788,43.4705,"Dummy address","Box 11719, 1135 Maple Way","Jackson,","Wyoming","WY",83002,"US",12313213213213,"+111111111","dummy ","dummy web address"`

console.log(csvJSON(csv))

function csvJSON(csv){

    var lines=csv.split("\n");

    var result = [];
    
    var commaRegex = /,(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)/g
    
    var quotesRegex = /^"(.*)"$/g

    var headers = lines[0].split(commaRegex).map(h => h.replace(quotesRegex, "$1"));

    for(var i=1;i<lines.length;i++){

        var obj = {};
        var currentline=lines[i].split(commaRegex);


        for(var j=0;j<headers.length;j++){

            obj[headers[j]] = currentline[j].replace(quotesRegex, "$1");
        }

        result.push(obj);

    }
    return result;
    //return JSON.stringify(result); 
  }

关于javascript - 将 CSV 转换为 JSON (JS) 时如何替换字符串中的逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53319890/

相关文章:

javascript - 在 dropzone.js 中启用复制和粘贴文件

javascript - JQuery SlideUp - 图像背后的背景颜色

C++ 如何根据分隔符序列将文件读入多维数组或三个单独的数组?

javascript - 显示对象的随机内容

javascript - 为什么我不应该从计算中设置数据?

javascript - 如何从 SVGSVGElement 创建 SVGPathElement

javascript - 我应该如何在 JavaScript 中设置循环引用

jquery - AJAX 获取请求失败 : JSONP

javascript - 哪个是最佳实践或更有效?

java - 将 String 转换为 Char 数组,然后仅返回数组的偶数索引