转换CSV文件到JSON使用C# [英] Converting a csv file to json using C#

查看:147
本文介绍了转换CSV文件到JSON使用C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有人写了一工具来转换CSV使用C#文件到JSON我想知道。从计算器前一个问题,我知道这个漂亮的实用工具 - https://github.com/cparker15/csv-到JSON的并此刻我打算提及,但现有的C#实现将是非常有益!谢谢!

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!

推荐答案

如果你可以使用 System.Web.Extensions程序,像这样可以工作:

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

您可能对CSV文件更复杂的分析要求,你可能有一个封装数据的类从一条线,但问题是,你可以序列化到JSON用一行代码,一旦你有行的集合。

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.

这篇关于转换CSV文件到JSON使用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆