1

I needed to convert my json to csv format. I downloaded newtonsoft.json dll from this link. My code looks like this

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();
    }
}

am i missing something here? Iam not able to deserialise the json object itself

Mohit S
  • 13,723
  • 6
  • 34
  • 69
Rakshith
  • 644
  • 1
  • 8
  • 24

0 Answers0