I would like to create a JSON file with the following format.
{
"Employee1": {
"Year1": {
"StartRange": 11,
"EndRange": 22
},
"Year2": {
"StartRange": 22,
"EndRange": 45
}
},
"Employee2": {
"Year1": {
"StartRange": 11,
"EndRange": 33
},
"Year2": {
"StartRange": 11,
"EndRange": 35
}
}
}
Here os my C# code to get the JSON Format
public void createFile(DataTable dTable)
{
string fileName = @"C:\Users\Documents\JSON\JsonConfig" + DateTime.Now.ToString("_dd_MM_yyyy_hh_mm_ss") + ".json";
List<string> EmployeeList= new List<string>();
ParentGroupList.Add("Employee1");
ParentGroupList.Add("Employee2");
if (!File.Exists(fileName))
{
FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
JsonTextWriter writer = new JsonTextWriter(sw);
JObject objChild = new JObject();
JObject objParent = new JObject();
foreach (var item in EmployeeList)
{
DataView dtView = dTable.DefaultView;
dtView.RowFilter = "EmployeeName='" + item.ToString() + "'";
DataTable dtNew = dtView.ToTable();
int Count = 0;
foreach (DataRow row in dtNew.Rows)
{
objChild = new JObject(
new JProperty(row["Year"].ToString(),
new JObject(
new JProperty("StartRange", Convert.ToInt32(row["StartRange"].ToString())), new JProperty("EndRange", Convert.ToInt32(row["EndRange"])))));
if (Count == 0 )
{
objParent.Merge(new JObject(new JProperty(item.ToString(), objChild)));
}
else
{
objParent.Merge(objChild);
}
Count++;
}
}
sw.Write(objParent);
sw.Close();
}
}
but the reusult JSON is like below with issues.
{
"Employee1": {
"Year1": {
"StartRange": 11,
"EndRange": 22
}
},
"Year2": {
"StartRange": 22,
"EndRange": 45
},
"Employee2": {
"Year1": {
"StartRange": 11,
"EndRange": 33
}
},
"Year2": {
"StartRange": 11,
"EndRange": 35
}
}
The DataTable will have the follwing table data
EmployeeName Years StartRange EndRange
Employee2 Year1 22 35
Employee2 Year2 35 48
Employee1 Year1 12 22
Employee1 Year2 22 32
What is the maistake I have done?. Please help me to sort out this issue.
Prevoiusly I was using DataTable and now I am Using List for keeping data from Database. here is the code and I am getting the JSON file with repeated values.
public class ConfigInfo
{
public string ParentGroup;
public string Label;
public int ID;
public int StartRange;
public int EndRange;
}
and creating List
List<ConfigInfo> configInfoList = new List<ConfigInfo>();
ConfigInfo configInfo = new ConfigInfo();
configInfo.ParentGroup = "Employee1";
configInfo.StartRange = 11;
configInfo.EndRange = 22;
configInfo.Label = "YYY";
configInfoList.Add(configInfo);
configInfo = new ConfigInfo();
configInfo.ParentGroup = "Employee2";
configInfo.StartRange = 24;
configInfo.EndRange = 56;
configInfo.Label = "XXX";
configInfoList.Add(configInfo);
if (!File.Exists(fileName))
{
FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
JsonTextWriter writer = new JsonTextWriter(sw);
Dictionary<string, Dictionary<string, LabelData>> data = GetData(configInfoList); // Here you do the reading
string json = JsonConvert.SerializeObject(data,Formatting.Indented);
sw.Write(json);
sw.Close();
}
and now GetData() is called
public Dictionary<string, Dictionary<string, LabelData>> GetData(List<ConfigInfo> configList)
{
var labelData = new Dictionary<string, Dictionary<string, LabelData>>();
foreach (var listItem in configList)
{
labelData[listItem.ParentGroup] = configList.Distinct().ToDictionary(x => x.Label.ToString(), row => new LabelData()
{
StartRange = Convert.ToInt32(listItem.StartRange.ToString()),
EndRange = Convert.ToInt32(listItem.EndRange.ToString())
});
}
return labelData;
}
and the resultant JSON as follows
{
"Employee1": {
"YYY": {
"StartRange": 11,
"EndRange": 22
},
"XXX": {
"StartRange": 11,
"EndRange": 22
}
},
"Employee2": {
"YYY": {
"StartRange": 24,
"EndRange": 56
},
"XXX": {
"StartRange": 24,
"EndRange": 56
}
}
}