簡體   English   中英

使用 ZEE9B16F39BC3629FB97E10A12A7B00 使用 header 記錄將 csv 序列化為 json

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

我有一個簡單的 csv,我正在嘗試將其序列化為 json。 如何將 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);

您可以使用Dictionary<string,string>列表,但您還需要從 csv 獲取 header 行。 我在下面給你一個粗略的想法。

        // 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);

以上將為您提供一個未包裝在 object 中的數組。 如果您想要將其包裝到 object 中的東西,您可以制作一個簡單的 class 來解決這個問題。 下面的例子。

    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);
            }
        }
    }

然后,您可以隨時使用以下內容輕松調用它。

        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