如何编写具有数据列表的JSON对象。 C#中的csv文件?

时间:2017-07-14 17:13:44

标签: c# json export-to-csv

我正在尝试找出一种方法,导出一个JSON对象,该对象具有一个报告列表,其中每个报告都有一个工作流列表到.CSV文件中。我查看了其他相关的解决方案,但dint发现它们很有帮助。请帮忙。

报告类:

 namespace TestApp
 {
     public class report
        {
            public string id { get; set; }
            public DateTime? date { get; set; }
            public bool completed { get; set; }
            public string status { get; set; }
            public List<worklog> worklogs { get; set; }

        }

 public class worklog
    {
        public string id { get; set; }
        public string subcontractor { get; set; }
        public int workerCount { get; set; }
        public double hours { get; set; }
        public string workdesc { get; set; }

    }
}

Reportcontainer类:

public class reportcontainer
{
    public List<report> reports { get; set; }

    public string totalHits { get; set; }

    public void WriteReportToCSV(string Filepath)
    {
        //Write all the data here to a .csv

    }

}

App.cs:

public reportcontainer GetReports()

        {

            Console.WriteLine("Reading Reports");

            reportcontainer result = null;

            try
            {
                string url = "https://app.ragenapp.com/api/v2/reports/?startDate=2017-06-07&endDate=2017-07-07&maxResults=10000&filters=project.id:119658&filters=project.id:119654&filters=project.id:119652";

                WebRequest request = WebRequest.Create(url);

                request.Method = "GET";
                request.Headers.Add("Authorization", "Bearer " + token);
                using (var str = new StreamReader(@"C:\projects\TestApp\App\result.json"))
                {
                    string tresult = str.ReadToEnd();

                    reportcontainer rc = JsonConvert.DeserializeObject<reportcontainer>(tresult);
          using (var sw = new StreamWriter(@"C:\Projects\TestApp\Report_Data.csv"))
                    {

                        if (rc != null)
                        {

                            foreach (report rp in rc.reports)
                            {
                                Console.WriteLine(" Reading: " + rp.id + "," + rp.status + "," + rp.completed + "," + rp.date);

                            }

                                foreach (worklog wl in rc.reports.SelectMany(r => r.worklogs))

                                {

                                    Console.WriteLine(" Reading: " + wl.id + "," + wl.hours + "," + wl.workerCount + "," + wl.workdesc);

                                }



                            foreach (report rep in rc.reports)

                            {
                                sw.WriteLine(" Reading:" + "," + rep.id + "," + rep.status + "," + rep.completed + "," + rep.date);

                            }

                                foreach (worklog wl in rc.reports.SelectMany(r => r.worklogs))

                                {

                                    sw.WriteLine(" Reading:" + wl.id + "," + wl.hours + "," + wl.workerCount + "," + wl.workdesc);

                                }


                        }
                    }
               }
            }
                      // }

                        catch (Exception ex)
                        {

                        }

            return result;  
 }

0 个答案:

没有答案