我需要将我的json转换为csv格式。我从this link下载了newtonsoft.json dll。我的代码看起来像这样
using System.IO;
using System;
using System.Data;
using Newtonsoft.Json;
class Program
{
static void Main()
{
// Read in every line in the file.
string text = System.IO.File.ReadAllText("input.txt");
XmlNode xml = JSON.DeserializeXmlNode("{records:{record:" + text + "}}");
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(xml.InnerXml);
var xmlReader = new XmlNodeReader(xmldoc);
DataSet dataSet = new DataSet();
dataSet.ReadXml(xmlReader);
var csv = dataSet.Tables[0].getCSV(",");
}
public static string getCSV(this DataTable table,string delimitor)
{
var result = new StringBuilder();
for (int i = 0; i < table.Columns.Count; i++)
{
result.Append(table.Columns[i].ColumnName);
result.Append(i == table.Columns.Count - 1 ? "\n" : delimitor);
}
foreach (DataRow row in table.Rows)
{
for (int i = 0; i < table.Columns.Count; i++)
{
result.Append(row[i].ToString());
result.Append(i == table.Columns.Count - 1 ? "\n" : delimitor);
}
}
return result.ToString().TrimEnd(new char[] { '\r', '\n' });
//return result.ToString();
}
}
我在这里错过了什么吗?我无法反序列化json对象本身