0

I want to write my JSON objects into a csv file in c# I'm using a web api from a windows 8.1 client, I tried this tutorial but "DeserializeXmlNode" seems to be missing, and XmlNode too! http://www.codeproject.com/Tips/565920/Create-CSV-from-JSON-in-Csharp

here's the code i use to get the api response:

protected override async void OnNavigatedTo(NavigationEventArgs e)          
{
        var server = "http://localhost:1504/api";
        var service = System.IO.Path.Combine(server, "PlayerStats");
        var playerstat = await GetAsync<IEnumerable<Common.Models.PlayerStat>>(new Uri(service));  

    }

    public async System.Threading.Tasks.Task<T> GetAsync<T>(Uri uri)
    {
        using (var http = new Windows.Web.Http.HttpClient())
        {
            http.DefaultRequestHeaders.Add("Accept", "application/json");
            var response = await http.GetAsync(uri);
            if (response.StatusCode != Windows.Web.Http.HttpStatusCode.Ok)
            {
                throw new Exception(response.StatusCode.ToString());
            }
            string json = await response.Content.ReadAsStringAsync();
            return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json);
        }
    }
}

I'm using System.XML and Newtonsoft.Json but why do I keem missing references, and is there a way to write my objects directy to a csv?

6
  • What does xml have to do with converting json to csv? Commented May 2, 2015 at 0:36
  • It's in the link, the guy used XmlNodeReader, he converted the json object to xml then to csv. I want to have csv but it's the only thing i found so far. I was trying a dump way using "foreach" to access evry object in my IEnumerable and write it's values separated by "," but the things is i can only access to a value by using "o.AttribueX", what if in my query i didnt select AttributeX? I'll be trying to write something that doesn't exist! Any better way to convert this? Commented May 2, 2015 at 0:53
  • You need the namespace System.Xml and a reference to System.Xml.dll Commented May 2, 2015 at 1:04
  • The namespace is there and System.xml.dll is already referenced by the build system. in the link he uses JSON.DeserializeXmlNode, in my case its JsonConvert.DeserializedXNode. There is no XmlNode it's XNode, besides for me (xmldoc.LoadXml(xml.InnerXml)) I have no InnerXml ! Commented May 2, 2015 at 1:26
  • Is your intent to write a general-purpose json - to - csv converter that will work with any data? That will be difficult because json is hierarchical and csv is flat. Commented May 2, 2015 at 7:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.