简体   繁体   English

JSON反序列化为List <string>

[英]JSON deserialization into List<string>

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. 我想使用WebClient请求WebAPI REST服务在.Net 2.0桌面应用程序中获取文件名List<string>List<string> )。

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: 服务中的WebAPI操作:

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> ? 如何将resultString (它是JSON String )转换为List<String>

Do I have to use JsonDataContractSerializer ? 我必须使用JsonDataContractSerializer吗?

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. 关键是引用Newtonsoft.Json命名空间(从NuGet包中添加它)并使用DeserializeObject泛型方法。 The answer shows other things you can do with results. 答案显示您可以对结果执行的其他操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM