Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Newtonsoft将CSV转换为JSON_C#_Json_Serialization_Json.net - Fatal编程技术网

C# 使用Newtonsoft将CSV转换为JSON

C# 使用Newtonsoft将CSV转换为JSON,c#,json,serialization,json.net,C#,Json,Serialization,Json.net,在你责怪我之前,是的,在我发布这个问题之前,我确实搜索过这个主题/问题 我的任务是使用C#&Newtonsoft将CSV转换为JSON。 然而,我在这个Newtonsoft文档中遇到了巨大的麻烦,不理解sh*t 我的想法是: 为程序提供CSV的路径 使用foreach循环读取CSV 将所有行添加到JSON对象 将JSON文件保存在同一文件夹中 这是我到目前为止得到的代码: using System; using System.IO; using System.Collections.Generi

在你责怪我之前,是的,在我发布这个问题之前,我确实搜索过这个主题/问题

我的任务是使用C#&Newtonsoft将CSV转换为JSON。 然而,我在这个Newtonsoft文档中遇到了巨大的麻烦,不理解sh*t

我的想法是:

  • 为程序提供CSV的路径
  • 使用foreach循环读取CSV
  • 将所有行添加到JSON对象
  • 将JSON文件保存在同一文件夹中
  • 这是我到目前为止得到的代码:

    using System;
    using System.IO;
    using System.Collections.Generic;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Converters;
    
    namespace CSVtoJson
    {
        class Program
        {
            static void Main(string[] args)
            {
                ConvertCsvFileToJsonObject();
                string ConvertCsvFileToJsonObject()
                {
                    string path = "C:\\Dev\\CSVtoJSON\\csvtojson.csv";
                    var csv = new List<string[]>();
                    var lines = File.ReadAllLines(path);
    
                    foreach (string line in lines)
                        csv.Add(line.Split(','));
    
                    var properties = lines[0].Split(',');
    
                    var listObjResult = new List<Dictionary<string, string>>();
    
                    for (int i = 1; i < lines.Length; i++)
                    {
                        var objResult = new Dictionary<string, string>();
                        for (int j = 0; j < properties.Length; j++)
                            objResult.Add(properties[j], csv[i][j]);
    
                        listObjResult.Add(objResult);
                    }
    
                    return JsonConvert.SerializeObject(listObjResult);
                }
            }
        }
    }
    
    使用系统;
    使用System.IO;
    使用System.Collections.Generic;
    使用Newtonsoft.Json;
    使用Newtonsoft.Json.Converters;
    名称空间CSVtoJson
    {
    班级计划
    {
    静态void Main(字符串[]参数)
    {
    ConvertCsvFileToJsonObject();
    字符串转换器CsvFileToJSonObject()
    {
    string path=“C:\\Dev\\CSVtoJSON\\CSVtoJSON.csv”;
    var csv=新列表();
    var lines=File.ReadAllLines(路径);
    foreach(行中的字符串行)
    csv.Add(line.Split(',');
    变量属性=行[0]。拆分(',');
    var listObjResult=新列表();
    对于(int i=1;i

    然而,我的函数似乎什么都不做,我也不知道如何创建JSON文件并保存它。。我非常感谢你给我的每一个评论,因为我在这件事上纠缠了好几个小时!!:)

    您读取csv的代码看起来可以工作。 只需使用Pavel Anikhouski评论中的信息 尝试更改下面的行,并在其后添加一行

    发件人:

    进入:


    您没有使用
    ConvertCsvFileToJsonObject()的返回值任意位置,以及写入文件。而且与json.net Library没有任何关系。您可以发布标题和CSV的一行吗?这是否回答了您的问题@viveknuna不我已经看过了。。谢谢你
    
    ConvertCsvFileToJsonObject();
    
    var jsonString = ConvertCsvFileToJsonObject();
    File.WriteAllText(""C:\\Dev\\CSVtoJSON\\output.json"", jsonString);