2016-05-12 108 views
0

我想用以下格式創建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 
    } 
    } 
} 
+0

什麼問題!輸出JSON對我來說很好看, – Dai

+1

你的錯誤是沒有清楚地提出你的問題 –

+0

大括號的排列方式在輸出不好 – Vinoth

回答

3

你的錯誤是你過度這一點。 我將放棄讀取數據的代碼,因爲它完全令人困惑。

你必須做的就是強烈定義輸出結構的事情:

public class YearData 
{ 
    public int StartRange { get; set; } 
    public int EndRange { get; set; } 
} 

你期望的輸出是一個Dictionary<string, Dictionary<string, YearData>>。 也就是說,一個以僱員爲關鍵字的字典,以及年/年數據對的字典作爲值。

當你擁有它,序列化很簡單:

Dictionary<string, Dictionary<string, YearData>> data = GetEmployeeData(); // Here you do the reading 
string json = JsonConvert.SerializeObject(data); 

並且序列完成。 所以,現在真正的問題是操縱你的數據來填充這個結構。但我認爲你可以設法自己做。

編輯:關於閱讀部分。 你也許可以做這樣的(未經測試,我不知道就算編譯,但應該給你一個快速啓動):

var employeeData = new Dictionary<string, Dictionary<string, YearData>>(); 
foreach(var employeeName in EmployeeList) 
{ 
    DataView dtView = dTable.DefaultView; 
    dtView.RowFilter = "EmployeeName='" + employeeName.ToString() + "'"; 
    DataTable dtNew = dtView.ToTable(); 

    employeeData[employeeName] = dtNew.Rows.ToDictionary(row => row["Year"].ToString(), row => new YearData() 
    { 
     StartRange = Convert.ToInt32(row["StartRange"].ToString())), 
     EndRange = Convert.ToInt32(row["EndRange"].ToString())) 
    }); 
} 

然後序列像上面。

+0

也許這也會有所幫助:http:// www。newtonsoft.com/json/help/html/SerializingCollections.htm – Nils

+0

感謝您的回覆。但是我無法理解您的代碼以及如何實現GetEmployeeData()的方法定義。 – Vinoth

+0

而你的代碼有一些語法錯誤。沒有編譯。 – Vinoth