简体   繁体   English

重组CSV数据以在JSON中创建正确的格式

[英]restructure CSV data to create correct format in JSON

I'm working with some CSV data. 我正在处理一些CSV数据。 Right now the CSV has a column called 'characteristic' which is one of three types, and a column called 'value', which contains the numerical value for the characteristic. 现在,CSV有一个名为“ characteristic”的列,这是三种类型之一,还有一个名为“ value”的列,其中包含该特征的数值。 I'd like to change the structure of the data so that the columns are the characteristics themselves, and the values fall directly under those columns. 我想更改数据的结构,以使列本身就是特征,而值直接位于这些列之下。 Here are screenshots of the tables, for clarity: 为了清楚起见,以下是表格的屏幕截图:

Currently: 目前: 在此处输入图片说明

What I'd like: 我想要的是: 在此处输入图片说明

I changed things manually to give an example. 我手动更改了示例。 The actual table I'll need to change is thousands of lines, so I'm hoping I can do this programmatically in some way. 我需要更改的实际表是数千行,所以我希望可以以某种方式以编程方式进行此操作。

The reason I need to restructure is that I need to transform the CSV to JSON, and the JSON needs to look like this: 我需要重组的原因是我需要将CSV转换为JSON,并且JSON需要如下所示:

    [
      {
        "country":"afghanistan",
        "iso3":"afg",
        "first_indicator":3,
        "second_indicator":5,
        "third_indicator":3
      },
      {
        "country":"united states",
        "iso3":"usa",
        "first_indicator":8,
        "second_indicator":6,
        "third_indicator":7
      },
      {
        "country":"china",
        "iso3":"chn",
        "first_indicator":6,
        "second_indicator":0.7,
        "third_indicator":2
      }
    ]

So - is there any way to take my CSV as it is now (first screenshot), and transform it to the JSON I want, without doing it all manually? 所以-有什么办法可以立即获取CSV文件(第一个屏幕截图),并将其转换为所需的JSON,而无需手动完成所有操作? I've done a lot of searching, and I think maybe I just don't know what to search for. 我已经做了很多搜索,我想也许我只是不知道要搜索什么。 Ideally I would use javascript for this, but any suggestions welcome. 理想情况下,我会为此使用javascript,但欢迎任何建议。 Thank you. 谢谢。

I made a JSFiddle for you, something like this should be what you want. 我为您制作了一个JSFiddle ,应该是您想要的。

JavaScript 的JavaScript

function Country(name, short){
    this["country"] = name;
    this["iso3"] = short;
}

function getCountryByName(name) {
    for(var i = 0; i < countries.length; i++){
        var country = countries[i];
        if(country["country"] == name){
            return country;   
        }
    }
    return null;
}

var csv = "country,shortname,characteristics,value\nafghanistan,afg,first_characteristic,3\nunited states,usa,first_characteristic,8\nchina,chn,first_characteristic,6\nafghanistan,afg,second_characteristic,5\nunited states,usa,second_characteristic,6\nchina,chn,second_characteristic,0.7\nafghanistan,afg,third_characteristic,3\nunited states,usa,third_characteristic,7\nchina,chn,third_characteristic,2"

var rows = csv.split("\n");
var countries = [];

if(rows.length > 0){
    var header = rows[0];
    var columns = header.split(",");
    var countryIndex = columns.indexOf("country");
    var shortnameIndex = columns.indexOf("shortname");
    var characteristicsIndex = columns.indexOf("characteristics");
    var valueIndex = columns.indexOf("value");

    for(var i = 1; i < rows.length; i++) {
        var row = rows[i];
        var columns = row.split(",");

        var name = columns[countryIndex];
        var short = columns[shortnameIndex];
        var characteristic = columns[characteristicsIndex];
        var value = columns[valueIndex];

        var country = getCountryByName(name);
        if(!country){
            country = new Country(name, short);   
            countries.push(country);
        }

        country[characteristic.replace("characteristic", "indicator")] = +value;
    }
}

console.log(countries);
console.log(JSON.stringify(countries));

Output from the last line is this: 最后一行的输出是这样的:

[{"country":"afghanistan","iso3":"afg","first_indicator":"3","second_indicator":"5","third_indicator":"3"},
{"country":"united states","iso3":"usa","first_indicator":"8","second_indicator":"6","third_indicator":"7"},
{"country":"china","iso3":"chn","first_indicator":"6","second_indicator":"0.7","third_indicator":"2"}]

My suggestion is to convert the CSV to JSON first. 我的建议是先将CSV转换为JSON。 You can use an online tool . 您可以使用在线工具 When you have the JSON you can write a Javascript code to modify the JSON in the format you want. 拥有JSON后,您可以编写Java代码以所需的格式修改JSON。

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

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