tom tom - 3 months ago 50
C# Question

C# Write json from CSV

I have got this code:

static void Main(string[] args)
{
DataTable datatable = new DataTable();
StreamReader streamreader = new StreamReader(@"C:\test\export.txt");
char[] delimiter = new char[] { '\t' };
string[] columnheaders = streamreader.ReadLine().Split(delimiter);
foreach (string columnheader in columnheaders)
{
datatable.Columns.Add(columnheader); // I've added the column headers here.
}

while (streamreader.Peek() > 0)
{
DataRow datarow = datatable.NewRow();
datarow.ItemArray = streamreader.ReadLine().Split(delimiter);
datatable.Rows.Add(datarow);
}

foreach (DataRow row in datatable.Rows)
{
Console.WriteLine("-------------------------");

foreach (DataColumn column in datatable.Columns)
{
//check what columns you need
if (column.ColumnName == "Type" ||
column.ColumnName == "Price")
{
Console.Write(column.ColumnName);
Console.Write(": ");
Console.WriteLine(row[column]);
}
}
}
Console.ReadLine();
}


Now I want the JSON output like:

{ "Type":the type-row
"Price": the price-row
}


Now I need the CSV File to be converted into a JSON-File. The CSV contains many headers but I only want "Type" and "Price" in the Output.

EDIT: Now I tried to use JSON.Net but I don^t get it working...

Daten datenobj = new Daten();
datenobj.Type = row[column].ToString;
datenobj.Price = row[column].ToString;
string jsondata = JsonConvert.SerializeObject(datenobj);
Console.WriteLine(jsondata);

}
}
}

Console.ReadLine();
}

public class Daten
{
public string Klasse { get; set; }
public string Datum { get; set; }


EDIT 2:

Daten datenobj = new Daten();
datenobj.Type = row[column].ToString();
datenobj.Price = row[column].ToString();
string jsondata = JsonConvert.SerializeObject(datenobj);
Console.WriteLine(jsondata);


It gives me the following output in the console:

{"Type":"rowoftype", "Price":"rowofprice"}
{"Type":"rowofprice", "Price":"rowofprice"}


New Code:

static void Main(string[] args)
{
DataTable datatable = new DataTable();
StreamReader streamreader = new StreamReader(@"C:\test\export.txt");
char[] delimiter = new char[] { '\t' };
string[] columnheaders = streamreader.ReadLine().Split(delimiter);
foreach (string columnheader in columnheaders)
{
datatable.Columns.Add(columnheader); // I've added the column headers here.
}

while (streamreader.Peek() > 0)
{
DataRow datarow = datatable.NewRow();
datarow.ItemArray = streamreader.ReadLine().Split(delimiter);
datatable.Rows.Add(datarow);
}

var datenList = new List<Daten>();
foreach (DataRow row in datatable.Rows)
{
var datenobj = new Daten()
{
Klasse = row["Klasse(n)"].ToString(),
Datum = row["Datum"].ToString()
};

datenList.Add(datenobj);
}

var jsondata = JsonConvert.SerializeObject(datenList);
Console.WriteLine(jsondata);

Console.ReadLine();
}

public class Daten
{
public string Klasse { get; set; }
public string Datum { get; set; }
}


I am getting an error with List();... It wants me to import Some more System.IO Stuff...

Answer

Create a list of your desired objects and then serialize it into the json you want

var datenList = new List<Daten>();
foreach (DataRow row in datatable.Rows) {
    var datenobj = new Daten() {
          Type = row["Type"].ToString(),
          Price = row["Price"].ToString()
    };

    datenList.Add(datenobj);
}

var jsondata = JsonConvert.SerializeObject(datenList);
Console.WriteLine(jsondata);

which should give you a json array of your objects

[
    {"Type":"rowoftype", "Price":"rowofprice"},
    {"Type":"rowofprice", "Price":"rowofprice"}
]

If the intention is to traverse the columns then you would need to use reflection for your object or construct the intended graph using a different object model

Comments