我想用以下格式創建JSON文件。來自C#對象的JSON字符串
{
"Employee1": {
"Year1": {
"StartRange": 11,
"EndRange": 22
},
"Year2": {
"StartRange": 22,
"EndRange": 45
}
},
"Employee2": {
"Year1": {
"StartRange": 11,
"EndRange": 33
},
"Year2": {
"StartRange": 11,
"EndRange": 35
}
}
}
這裏OS我的C#代碼來獲得JSON格式
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();
}
}
但reusult JSON是像下面的問題。
{
"Employee1": {
"Year1": {
"StartRange": 11,
"EndRange": 22
}
},
"Year2": {
"StartRange": 22,
"EndRange": 45
},
"Employee2": {
"Year1": {
"StartRange": 11,
"EndRange": 33
}
},
"Year2": {
"StartRange": 11,
"EndRange": 35
}
}
的數據表將有follwing表數據
EmployeeName Years StartRange EndRange
Employee2 Year1 22 35
Employee2 Year2 35 48
Employee1 Year1 12 22
Employee1 Year2 22 32
什麼是maistake我已經做了?請幫我解決這個問題。
Prevoiusly我正在使用DataTable,現在我正在使用List來保存來自數據庫的數據。這裏是代碼,我得到重複值的JSON文件。
public class ConfigInfo
{
public string ParentGroup;
public string Label;
public int ID;
public int StartRange;
public int EndRange;
}
和創建列表
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();
}
現在的GetData()被調用
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;
}
並將所得JSON如下
{
"Employee1": {
"YYY": {
"StartRange": 11,
"EndRange": 22
},
"XXX": {
"StartRange": 11,
"EndRange": 22
}
},
"Employee2": {
"YYY": {
"StartRange": 24,
"EndRange": 56
},
"XXX": {
"StartRange": 24,
"EndRange": 56
}
}
}
什麼問題!輸出JSON對我來說很好看, – Dai
你的錯誤是沒有清楚地提出你的問題 –
大括號的排列方式在輸出不好 – Vinoth