2

I guess this is easy, but I am new and cannot find a fast answer to this:

I want to obtain a list of file names (List<string>) in a .Net 2.0 desktop app, using WebClient requesting to a WebAPI REST service.

So, I have this code in the desktop app:

using (var client = new WebClient())
{
   client.Headers[HttpRequestHeader.Accept] = "application/json";
   var resultString = client.DownloadString("http://localhost:3788/api/file?src=X&dest=Y");
}

and a WebAPI action like this in the service:

public IEnumerable<string> GetFiles([FromUri]string src, [FromUri]string dest)
{
   // some code here
}

How would I convert the resultString, which is a JSON String, to a List<String> ?

Do I have to use JsonDataContractSerializer?

Learner
  • 3,297
  • 4
  • 37
  • 62

1 Answers1

1

Look at this SO question and answer. The code there outlines what you would do. The key is to reference the Newtonsoft.Json namespace (add it from a NuGet package) and use the DeserializeObject generic method. The answer shows other things you can do with results.

Community
  • 1
  • 1
Sixto Saez
  • 12,610
  • 5
  • 43
  • 51