Json到csv json.net C#反序列化问题 [英] Json to csv json.net C# deserialization problem
问题描述
将Json转换为CSV
我正在使用json .net库
我有大json文件,需要转换为.csv文件我可以将数组反序列化为数据表和
datatable to csv。
如何将整个文件反序列化为csv?它有列表,数组一些根对象
我使用json2csharp从json文件生成类
json文件结构;
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; }
}
我只获取数据网格视图中的列和空行。(null)
如何解析多层Json到CSV
我尝试过:
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;
// char seperator = ',';
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) ;//call function to convert list to datatable
dataGridView1.DataSource = dTable;
}
return json;
}
有了这个,我在数据表中得到3列(名称,参数地址,值),但它们是空的。 .no row data。
With this I am getting 3 columns (Name,Parameter Address, Value)in datatable but they are empty ..no row data.
推荐答案
以下是url的一些示例:
在C#中用JSON创建CSV [ ^ ]
JSON字符串到CSV c#中的CSV到JSON转换 - Stack Overflow [ ^ ]
Here are some examples at following url's :
Create CSV from JSON in C#[^]
JSON string to CSV and CSV to JSON conversion in c# - Stack Overflow[^]
这篇关于Json到csv json.net C#反序列化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!