2016-11-10 82 views
0

我有一個非常嵌套的大量json文件。我需要根據特定字段的名稱編寫多個csv文件,如果它存在,那麼將值添加到我創建的頭文件中,如果它不創建新文件的話。這工作得很好。然而,我遇到了一個問題,其中標題不匹配,因爲該記錄不存在此特定標題。例如:從json創建每個記錄具有不同標題值的csv文件

Header: Dog Cat Mouse Horse 
Record1: yes yes yes yes 

//上述與添加記錄兩個的所有值 其中標頭值根本沒有被

Header: Dog Cat Mouse Horse 
Record1: yes yes yes yes 
Record2: yes yes yes *** 

RECORD2中列出的文件的一個例子以上沒有對小鼠記錄,但因爲它沒有排隊是向左移動。在將值傳遞給文件之前,我需要在頭文件之下寫入一個Null。下面是我的代碼,如果你能幫助將是巨大的,因爲我在這一點上失去了:

 static List<string> headList = new List<string>(); 
     static List<string> newHeaderList = new List<string>(); 
     static List<string> valueList = new List<string>(); 
     static List<string> oldHeadList = new List<string>(); 
     static void Main() 
     { 
      var data = JsonConvert.DeserializeObject<dynamic>(File.ReadAllText(
          @"C:\Users\nphillips\workspace\2016R23\UITestAutomation\SeedDataGenerator\src\staticresources\seeddata.resource")); 
      string fileName = ""; 
      var bundles = data.RecordSetBundles; 

      foreach (var bundle in bundles) 
      { 
       var records = bundle.Records; 
       foreach (var record in records) 
       { 
        var test = record.attributes; 
        foreach (var testagain in test) 
        { 
         // Getting the object Name Ex. Location, Item, etc. 
         var jprop = testagain as JProperty; 
         if (jprop != null) 
         { 
          fileName = jprop.First.ToString().Split('_')[2] + ".csv"; 
         } 
         break; 
        } 
        string header = ""; 
        string value = ""; 
        foreach (var child in record) 
        { 
         var theChild = child as JProperty; 
         if (theChild != null && !theChild.Name.Equals("attributes")) 
         { 
          // adding the name and values to list 
          headList.Add(child.Name); 
          valueList.Add(child.Value.ToString()); 
         } 
        } 
        // calling method to write columns and values 
        writeCSV(headList, valueList, fileName); 
        valueList.Clear(); 
        headList.Clear(); 
       } 
      } 
     } 

     public static void writeCSV(List<string> headList, List<string> valList, string fileName) 
     { 
      string headerString = ""; 
      string value = ""; 

      if (!File.Exists(fileName)) 
      { 
       foreach (var header in headList) 
       { 
        foreach (var val in valList) 
        { 
         value += val + ","; 
        } 
        oldHeadList.Add(header); 
        headerString += header + ','; 
       } 

       headerString += "+" + Environment.NewLine; 
       File.WriteAllText(fileName, headerString); 
      } 
      else 
      { 
       foreach (var header in headList) 
       { 
        foreach (var oldHeader in oldHeadList) 
        { 
         foreach (var val in valList) 
         { 
          if (header != oldHeader) 
          { 
           value += "null,"; 
          } 
          else 
          { 
           value += val + ","; 
          } 
         } 

        } 
       } 
      } 
      File.AppendAllText(fileName, value); 
      value += Environment.NewLine; 
     } 
    } 

,我不能爲改變我的可怕的JSON文件的使用我公司:https://codeshare.io/rGL6K5

回答

0

有某種模式? 我問的原因是,也許你可以創建一個服務,將JSON序列化爲一個複雜的對象。一旦完成了一個將該對象序列化爲csv的服務。該服務將根據需要知道寫入多個csv文件。

相關問題