Convert Json to CSV
I am using json .net library
I have big json file and need to convert to .csv file I can able to deserialized array to datatable and
datatable to csv.
how to deserialized whole file to csv ? it has list,array some rootobjects
I have generated classes from json file using "json2csharp"
json file structure;
{
"Active":"0",
"Customer_Reference":"Suncor",
"Datalink_Bus":"J1939",
"Engine_Serial_Number":"66302847",
"Equipment_ID":"0300",
"FMI":"1",
"Latitude":"56.727",
"Longitude":"-111.381",
"Message_Type":"FC",
"Notification_Version":"2.004.002",
"Occurrence_Count":"1",
"Occurrence_Date_Time":"2016-10-11T14:34:38.150Z",
"SPN":"100",
"Sent_Date_Time":"2016-10-12T00:52:28.825Z",
"Snapshots":
[{
"Parameter":
[{
"Name":"Truck speed",
"Parameter_Source_Address":"",
"Value":"0"
},
{
"Name":"HP Feedback - Alt In",
"Parameter_Source_Address":"",
"Value":"7"
}],
"Snapshot_DateTimestamp":"2016-10-11T14:34:09.000Z"
},
{
"Parameter":
[{
"Name":"Truck speed",
"Parameter_Source_Address":"",
"Value":"0"
},
{
"Name":"HP Feedback - Alt In",
"Parameter_Source_Address":"",
"Value":"7"
}],
"Snapshot_DateTimestamp":"2016-10-11T14:34:10.000Z"
}]
}
Classes:
public class Parameter
{
public string Name { get; set; }
public string Parameter_Source_Address { get; set; }
public string Value { get; set; }
}
public class Snapshot
{
public List<parameter> Parameter { get; set; }
public string Snapshot_DateTimestamp { get; set; }
}
public class RootObject
{
public string Active { get; set; }
public string Customer_Reference { get; set; }
public string Datalink_Bus { get; set; }
public string Engine_Serial_Number { get; set; }
public string Equipment_ID { get; set; }
public List<snapshot> Snapshots { get; set; }
}
I am am getting only columns in datagrid view and empty rows.(null)
how do i parse multilayer Json TO CSV
What I have tried:
public string ReadJsonFile(String Filepath)
{
string json = string.Empty;
using (StreamReader read = new StreamReader(textBox1.Text))
{
json = read.ReadToEnd();
var data1 = JsonConvert.DeserializeObject<parameter>(json);
List<parameter> data = JsonConvert.DeserializeObject<list<parameter>>(json);
DataTable dTable = ToDataTable(data) ;
dataGridView1.DataSource = dTable;
}
return json;
}
With this I am getting 3 columns (Name,Parameter Address, Value)in datatable but they are empty ..no row data.