webclient 类提供了向由 uri 标识的任何本地、intranet 或 internet 资源发送数据或从其接收数据的常用方法。
webclient 类使用 webrequest 类提供对资源的访问。 webclient 实例可以使用通过 webrequest.registerprefix 方法注册的任何 webrequest 后代访问数据。
downloadstring 从资源下载字符串并返回字符串。
如果您的请求需要可选标头,您必须将标头添加到 headers 集合中
示例在下面的示例中,我们将 url 称为“https://” jsonplaceholder.typicode.com/posts
然后将示例反序列化为 user 数组
从用户数组中我们得到打印第一个数组值
示例class program{ static void main(string[] args){ var client = new webclient(); var json = client.downloadstring("https://jsonplaceholder.typicode.com/posts"); var userposts = jsonconvert.deserializeobject<user[]>(json); system.console.writeline(userposts[0].title); console.readline(); }}public class user{ public string userid { get; set; } public string id { get; set; } public string title { get; set; } public string body { get; set; }}
输出sunt aut facere repellat provident occaecati excepturi optio reprehenderit
以上就是如何在 c# 中使用 newtonsoft json 将 json 反序列化为 .net 对象并从数组中仅选择一个值?的详细内容。