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?

2
  • Where is the contents of the JSON string? Commented Jan 18, 2014 at 23:08
  • I have to make sure I return only the filenames first and not including the entire path, so I have to make some adjustments Commented Jan 18, 2014 at 23:12

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for this, I will give it a try. So, there are these 2 libs: Newtonsoft.Json and JSON.NET. Any preference? is there something built-in .net?
@Cristi they are the same.
Yes, @L.B. is right. The .NET namespace is Newtonsoft.Json but it's probably better known as the Json.NET library.
Oh... this shows you how noob I am :) Thanks. Let's see if I get the expected results
Yeah, it worked nicely using: JsonConvert.DeserializeObject<List<string>>(resultString);

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.