I have tried something like this , Please alter according to your usage:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Newtonsoft.Json;
using System.IO;
namespace WindowsFormsApplication1
{
public class AlteredUsers
{
public int UserID;
public string userName;
}
public class AttributeDetails
{
public string Abbreviation;
public string DataType;
public string DefaultOperator;
public int DefaultStatus;
public bool DefaultStatusSpecified;
public string DisplayName;
public int EffectiveStatus;
public bool EffectiveStatusSpecified;
public string HelpText;
public int ID;
public bool IDSpecified;
public bool IsQualifier;
}
public class MyJSONClass
{
public int AlertID;
public bool AlertIDSpecified;
public string AlertSentTimeStamp;
public bool AlertSentTimeStampSpecified;
public List<AlteredUsers> Altereduser;
public AttributeDetails Attributedetails;
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnConvertToCsv_Click(object sender, EventArgs e)
{
MyJSONClass objMyJSONClass = new MyJSONClass();
objMyJSONClass.Altereduser = new List<AlteredUsers>();
objMyJSONClass = ConvertJSONToObject(textBox1.Text);
if (null != objMyJSONClass)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "CSV File|*.csv";
saveFileDialog1.Title = "Save CSV File";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
SaveToCSV(saveFileDialog1.FileName, objMyJSONClass);
}
}
}
private void SaveToCSV(string csvfileName, MyJSONClass objMyJSONClass)
{
string csvString = objMyJSONClass.AlertID+","+
objMyJSONClass.AlertIDSpecified+","+
objMyJSONClass.AlertSentTimeStamp+","+
objMyJSONClass.AlertSentTimeStampSpecified;
if (null != objMyJSONClass.Altereduser)
{
foreach (AlteredUsers user in objMyJSONClass.Altereduser)
{
csvString = "," + user.UserID.ToString() + "," + user.userName;
}
}
csvString = csvString + "," + objMyJSONClass.Attributedetails.Abbreviation + "," +
objMyJSONClass.Attributedetails.DataType + "," +
objMyJSONClass.Attributedetails.DefaultOperator + "," +
objMyJSONClass.Attributedetails.DefaultStatus.ToString() + "," +
objMyJSONClass.Attributedetails.DefaultStatusSpecified.ToString() + "," +
objMyJSONClass.Attributedetails.DisplayName + "," +
objMyJSONClass.Attributedetails.EffectiveStatus.ToString() + "," +
objMyJSONClass.Attributedetails.EffectiveStatusSpecified.ToString() + "," +
objMyJSONClass.Attributedetails.HelpText + "," +
objMyJSONClass.Attributedetails.ID.ToString() + "," +
objMyJSONClass.Attributedetails.IDSpecified.ToString() + "," +
objMyJSONClass.Attributedetails.IsQualifier.ToString();
File.WriteAllText(csvfileName, csvString);
}
private MyJSONClass ConvertJSONToObject(string InputJSONString)
{
MyJSONClass objMyJSONClass = new MyJSONClass();
objMyJSONClass.Altereduser = new List<AlteredUsers>();
try
{
objMyJSONClass = (MyJSONClass)JsonConvert.DeserializeObject(InputJSONString, objMyJSONClass.GetType());
return objMyJSONClass;
}
catch (Exception ex)
{
return null;
}
}
}
}
please correct your JSON Like this :
{
"AlertID":20515,
"AlertIDSpecified":true,
"AlertSentTimeStamp":"Date(-2208992400000)",
"AlertSentTimeStampSpecified":true,
"AlertedUsers":[],
"AttributeDetails":
{
"Abbreviation":"Status",
"DataType":"string",
"DefaultOperator":"=",
"DefaultStatus":4,
"DefaultStatusSpecified":true,
"DisplayName":"Status",
"EffectiveStatus":4,
"EffectiveStatusSpecified":true,
"HelpText":"Indicates the status of the check execution",
"ID":10400,
"IDSpecified":true,
"IsQualifier":true
}
}