C# CSV转成JSON

需要引用命名空间:using Newtonsoft.Json.Linq;

先来介绍几个类:

类名说明
JObject用于操作JSON对象
JArray用于操作JSON数组
JProperty

表示对象中的属性,以“Key/Value”形式

  
JObject:基本的json对象:
public JObject GetJObject()
 {
  //JObject obj = new JObject(new JProperty("Name", "Mark"));
  JObject  obj = new JObject{{"Name", "Mark" } };     //效果一样
  return obj;
 }

JObject:嵌套子对象(JObject嵌JObject)

 public JObject GetJObject()
        {
            JObject obj = new JObject(new JProperty("Name", "Mark"), new JProperty("Age", 8), new JProperty("Info", new JObject(new JProperty("Phone", "132****7777"),  new JProperty("Gender", "男"))));
            //JObject obj = new JObject { { "Name", "Mark" }, { "Age", 8 } };
            //JObject info = new JObject { { "Phone", "132****7777" }, { "Gender", "男" } };
            //obj.Add("Info", info);
            return obj;
        }

JArray:基本json对象中的数组

public JArray GetJArray()
        {
            JArray jarray = new JArray();
            JObject mark = new JObject { { "Name", "Mark" }, { "Age", 8 } };
            JObject jack = new JObject { { "Name", "Jack" }, { "Age", 9 } };
            jarray.Add(mark);
            jarray.Add(jack);
            return jarray;
        }

JArray: 多个json对象数组

        public JObject GetJArray()
        {
            var obj = new JObject();
            var student = new JArray
            {
                new JObject {{ "Name", "Mark" }, { "Age", 8 } },
                new JObject {{ "Name", "Jack" }, { "Age", 9 } }
            };
            var results = new JArray
            {
                new JObject {{ "Subject", "语文"}, { "Score", 100}},
                new JObject {{ "Subject", "数学" }, { "Score", 88}}
             };
            obj.Add("Student", student);
            obj.Add("Results", results);
            return obj;
        }

下面就来写一个简单的CSV转JSON:

csv文件:


        /// <summary>
        /// CSVToJSON
        /// </summary>
        /// <param name="filePath">CSV文件路径</param>
        public void CsvToJson(string filePath)
        {
            JObject sta = new JObject();          //新建一个JObject对象
            sta.Add(new JProperty("starttime", "20180101"));
            JArray arr = new JArray();                  //新建一个JArray对象
            Dictionary<string, int> lstTrueField = new Dictionary<string, int>();  //新建键值对对象
            StreamReader reader = new StreamReader(filePath, System.Text.Encoding.Default, false);    //读取CSV文件
            int m = 0;
            while (!reader.EndOfStream)
            {
                m = m + 1;
                string str = reader.ReadLine();
                string[] split = str.Split(',');
                if (m == 1)                           //第一行作为列名
                {
                    for (int c = 3; c < split.Length; c++)
                    {
                        lstTrueField.Add(split[c], c);    //课程与列序号
                    }
                }
                if (m > 1)
                {
                    arr.Add(new JObject(new JProperty("Name", split[0]), new JProperty("Age", split[1]),new JProperty("Sex",split[2]), new JProperty("course", newJArray(new JObject(from p in lstTrueField

                                                                                                                                                                         select  new JProperty(p.Key,split[p.Value]))))));
                }
            }
            reader.Close();   //关闭流
            sta.Add(new JProperty("locdata", arr));  //将JArray数组加入JObject对象内。
        }

转JSON时类似的状况会经常遇到

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值