使用C#将csv文件转换为json [英] Converting a csv file to json using C#
问题描述
我想知道是否有人写了一个工具来将CSV文件转换为Json使用C#。从上一个关于stackoverflow的问题,我知道这个很好的实用程序 - https://github.com / cparker15 / csv-to-json ,目前我计划引用它,但现有的C#实现将是非常有帮助的!
如果您可以使用 System.Web.Extensions
,那么您可以使用
var csv = new List< string []>(); //或,List< YourClass>
var lines = System.IO.File.ReadAllLines(@C:\file.txt);
foreach(string line in lines)
csv.Add(line.Split(',')); //或,填充YourClass
string json = new
System.Web.Script.Serialization.JavaScriptSerializer()。Serialize(csv);
您可能对csv文件有更复杂的解析需求,您可能有一个类封装数据从一行,但问题是,一旦你有一个行集合,你可以用一行代码序列化到JSON。
I was wondering if someone's written a utility to convert a CSV file to Json using C#. From a previous question on stackoverflow, I'm aware of this nice utility - https://github.com/cparker15/csv-to-json and at the moment I plan to refer to it but an existing C# implementation would be very helpful! Thanks!
If you can use System.Web.Extensions
, something like this could work:
var csv = new List<string[]>(); // or, List<YourClass>
var lines = System.IO.File.ReadAllLines(@"C:\file.txt");
foreach (string line in lines)
csv.Add(line.Split(',')); // or, populate YourClass
string json = new
System.Web.Script.Serialization.JavaScriptSerializer().Serialize(csv);
You might have more complex parsing requirements for the csv file and you might have a class that encapsulates the data from one line, but the point is that you can serialize to JSON with one line of code once you have a Collection of lines.
这篇关于使用C#将csv文件转换为json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!