简体   繁体   English

使用 ZEE9B16F39BC3629FB97E10A12A7B00 使用 header 记录将 csv 序列化为 json

[英]Serialize a csv to json with header record using json.net newtonsoft

I have a simple csv that I am trying to serialize to json.我有一个简单的 csv,我正在尝试将其序列化为 json。 How do I include the header record as the name in the name value pairs?如何将 header 记录作为名称包含在名称值对中?

  JsonSerializer serializer = new JsonSerializer();

  var csv = new List<string[]>();
  var lines = System.IO.File.ReadAllLines(file);
  foreach (string line in lines)
    csv.Add(line.Split(','));

  string json = JsonConvert.SerializeObject(csv, Formatting.Indented);

You can use a List of Dictionary<string,string> but you also need to get the header row from the csv.您可以使用Dictionary<string,string>列表,但您还需要从 csv 获取 header 行。 I have a rough idea for you below.我在下面给你一个粗略的想法。

        // initialize the list
        var list = new List<Dictionary<string, string>>();
        var lines = System.IO.File.ReadAllLines("");
        // get your header values
        var headers = lines[0].Split(',');

        // Here you want to skip the first line because it is the header
        foreach (string line in lines.Skip(1))
        {
            // split your line to get individual values
            var lineSplit = line.Split(',');
            // make a dictionary to hold the line values
            var dictionary = new Dictionary<string, string>();
            // do a for loop to apply your headers
            for (int i = 0; i < headers.Length; i++)
            {
                dictionary.Add(headers[i], lineSplit[i]);
            }
            // Add your dictionary to the list
            list.Add(dictionary);
        }

        string json = JsonConvert.SerializeObject(list, Formatting.Indented);

The above will give you an array that is not wrapped in an object.以上将为您提供一个未包装在 object 中的数组。 If you would like something that will wrap it into an object you can just make a simple class to take care of that.如果您想要将其包装到 object 中的东西,您可以制作一个简单的 class 来解决这个问题。 Example below.下面的例子。

    public class CsvToJson
    {
        public CsvToJson()
        {
            this.List = new List<Dictionary<string, string>>();
        }
        public CsvToJson(string filePath)
        {
            this.List = new List<Dictionary<string, string>>();
            // Adding some exception prevention
            if (File.Exists(filePath))
            {
                ConvertFromCsvToJson(filePath);
            }
        }

        public List<Dictionary<string, string>> List { get; set; }

        private void ConvertFromCsvToJson(string filePath)
        {
            var lines = File.ReadAllLines(filePath);
            // get your header values
            var headers = lines[0].Split(',');

            foreach (string line in lines.Skip(1))
            {
                // split your line to get individual values
                var lineSplit = line.Split(',');
                // make a dictionary to hold the line values
                var dictionary = new Dictionary<string, string>();
                // do a for loop to apply your headers
                for (int i = 0; i < headers.Length; i++)
                {
                    dictionary.Add(headers[i], lineSplit[i]);
                }
                // Add your dictionary to the list
                List.Add(dictionary);
            }
        }
    }

Then you could easily call on this anytime by using something like below.然后,您可以随时使用以下内容轻松调用它。

        var rootObject = new CsvToJson("C:\testFile.csv");
        var json = JsonConvert.SerializeObject(rootObject, Formatting.Indented);

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

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