From 6129a9cb6230aafa810ad9a1a01f2076c8d5cef6 Mon Sep 17 00:00:00 2001 From: parsaManouchehrian Date: Sat, 20 Oct 2018 12:55:51 -0700 Subject: [PATCH] add csvToJson --- Groot/Groot.cs | 25 +++++++++++++++++++++++-- Groot/Groot.csproj | 1 + 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/Groot/Groot.cs b/Groot/Groot.cs index 7d917b9..3c1592e 100644 --- a/Groot/Groot.cs +++ b/Groot/Groot.cs @@ -1,4 +1,5 @@ -using System; +using Newtonsoft.Json.Linq; +using System; using System.Collections; using System.Collections.Generic; using System.Collections.Immutable; @@ -78,6 +79,26 @@ private static object ChangeType(PropertyInfo prop, string value) return Convert.ChangeType(value, prop.PropertyType); } + public static string csvToJson(string filePath) + { + var csvlines = File.ReadAllLines(filePath); + var header = csvlines[0].Split(',').Select(s => s.Trim()).ToList(); + JArray array = new JArray(); + for (int i = 1; i < csvlines.Length; i++) + { + JObject jObject = new JObject(); - } + var col = csvlines[i].Split(',').Select(s => s.Trim()).ToList(); + for (int j = 0; j < header.Count; j++) + { + jObject.Add(header[j], col[j]); + } + + array.Add(jObject); + + } + + return array.ToString(); + } + } } \ No newline at end of file diff --git a/Groot/Groot.csproj b/Groot/Groot.csproj index 8fddb85..de77559 100644 --- a/Groot/Groot.csproj +++ b/Groot/Groot.csproj @@ -5,5 +5,6 @@ + \ No newline at end of file