none
Creating a CSV file from API Get Data RRS feed

  • Question

  • Hi,

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

    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;
            }
    
        }

    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!

    Tuesday, June 5, 2018 9:54 AM

All replies