从API获取数据创建CSV文件 [英] Creating a CSV file from API Get Data

查看:240
本文介绍了从API获取数据创建CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#和.Net平台的新手(从Javascript世界过来),所以任何帮助/样本都会很棒。 

I'm very new to C# and the .Net platform(crossing over from Javascript world), so any help/samples would be great. 

高级开发人员编写了一个程序,该程序目前使用API​​中的数据并转换为JSON格式。我现在需要编写一个方法来获取这些数据并在收集数据后创建一个CSV(;)分隔文件。

A senior developer has written a program, that currently consumes data from an API and converts to a JSON format. I now need to write a method that will take this data and create a CSV (;) delimited file after the data is gathered.

任何关于如何实现这一点的想法,或者这是否可能?

Any thoughts on how to accomplish this, or is this even possible?

public class ConvertToCsv
    {
        private GetUserData GetUserFromRest(SessionData sessionData, string userId)
        {
            var restProxy = new CsodRestProxy();
            GetUserData getUserData = null;

            var v = restProxy.GetUser(sessionData.Token, sessionData.Secret, ENV.EnvironmentName, null, userId);
            try
            {
                getUserData = JsonConvert.DeserializeObject<ENV.GetUserData>(v);

            }
            catch (Exception) { }


            return getUserData;
        }

    }

还有RestProxy.cs实际通过API进行身份验证并返回数据。这是代码。这是我应该构建CreateCSV方法吗?

There is also the RestProxy.cs that actually authenticates with the API and returns the data. Here's the code. Is this were I should build the CreateCSV method?

            string responseFromServer = "";
            try
            {
                    using (StreamReader reader = new StreamReader(request.GetResponse().GetResponseStream()))
                    {
                        responseFromServer = reader.ReadToEnd();
                  

                    }

            }
            catch(WebException we)
            {
                var reader = new StreamReader(we.Response.GetResponseStream());
                responseFromServer = reader.ReadToEnd();
            }
            catch (Exception e) {
                responseFromServer = (e.Message + " \n" + e.StackTrace);

            }

            return responseFromServer;
        }


感谢任何帮助!

Thankful for any help!

推荐答案

请问为什么?定义好的格式如JSON或XML有什么问题?特别是后者可以通过内置的.NET XML对象序列化轻松使用。

May I ask why? What's wrong with well-defined formats like JSON or XML? Especially as the later can be easily used thru the built-in .NET XML serialization of objects.


这篇关于从API获取数据创建CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆