简体   繁体   中英

CSV files to JSON and then back to CSV

I'm trying to create a page that is going to read a CSV file and create a JSON file. I need two of the columns in the json. The CSV contains between 15,000-30,000 lines. I need the JSON to be max 10,000 lines and then I am going to check the JSON against an API. The result must get values from the original file, and then write it to a new CSV file with all the columns.

The CSV file looks like this:

number,"surname","forename","emailAddress","taxIdentifier"
101719008,"John","smith","smith@hotmail.com","1997xxxxxxxx"
102358612,"John","doe","doe@gmail.com","1993xxxxxxxx"

I have this code to read the CSV file:

protected void Button2_Click(object sender, EventArgs e)
{
    string saved = (@"E:\Temp\Spelpaus\Malmö2022fromNeon.csv");

    using (var streamreader = new StreamReader(saved))
    using (var csvReader = new CsvReader(streamreader, CultureInfo.InvariantCulture))
    {
        var records = csvReader.GetRecords<RocketLaunch>().ToList();
    }
}

public class RocketLaunch
{
    public string number { get; set; }
    public string surname { get; set; }
    public string forename { get; set; }
    public string emailAddress { get; set; }
    public string taxIdentifier { get; set; }
}

I need a start in the JSON and then 2 columns (number and taxidentifier) from the CSV and then 2 characters at the end.

After I got the answer from the API I need to find number from original list and get all columns for every number from API.

Any ideas?

You've defined CSVEntry.Parse as static, so the call could look something like:

string line = rd.ReadLine();
CSVEntry entry = CSVEntry.Parse(line);

You could add entry to a list of type List<CSVEntry> . One issue might be that your sample data is comma separated, but your code is splitting on semicolons.

Split is a good method to get you quickly started, but I notice that some fields are quoted, which means there's the potential for the data within the quotes to include commas. I wrote my own CSV parser many years ago to handle this sort of thing, but I'm sure there must be something on NuGet to do this for you these days (look for something that supports RFC 4180).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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