ASP.NET Web API将http响应转换为json数组
发布时间:2020-12-15 23:07:47 所属栏目:asp.Net 来源:网络整理
导读:调用REST URL时,我的代码工作正常: http://api.feedzilla.com/v1/categories.json 但当我调用以下URL时,我收到错误: http://api.feedzilla.com/v1/categories/15/articles.json?count=36since=2012-11-15client_source=order=relevancetitle_only=0 错误:
调用REST URL时,我的代码工作正常:
http://api.feedzilla.com/v1/categories.json 但当我调用以下URL时,我收到错误: 错误: {"Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.IEnumerable`1[Nitin.News.DAL.Resources.Article]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.rnTo fix this error either change the JSON to a JSON array (e.g. [1,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer,not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.rnPath 'articles',line 1,position 12."} 我的守则如下: public class Article { public string publish_date { get; set; } public string source { get; set; } public string source_url { get; set; } public string summary { get; set; } public string title { get; set; } public string url { get; set; } } public IEnumerable<Article> Get() { HttpClient client = new HttpClient(); client.BaseAddress = new Uri("http://api.feedzilla.com/v1/"); //Add an Accept header for JSON format. client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // call the REST method HttpResponseMessage response = client.GetAsync("http://api.feedzilla.com/v1/categories/2/articles.json??count=36&since=2012-11-15&client_source=&order=relevance&title_only=0&").Result; // Blocking call! if (response.IsSuccessStatusCode) { // Parse the response body. Blocking! return response.Content.ReadAsAsync<IEnumerable<Article>>().Result; //wont work //string JSON =response.Content.ReadAsStringAsync().Result; //return JsonConvert.DeserializeObject<IEnumerable<T>>(JSON); } else { throw new Exception(string.Format("Data access faild,{0} ({1}) method:{2}",(int)response.StatusCode,response.ReasonPhrase,MethodURL)); } } 解决方法
你的对象层次需要另一个级别…即包含IEnumerable的根.
在http://json2csharp.com/运行JSON到工具会生成以下代理类: public class Enclosure { public int length { get; set; } public string media_type { get; set; } public string uri { get; set; } } public class Article { public string author { get; set; } public string publish_date { get; set; } public string source { get; set; } public string source_url { get; set; } public string summary { get; set; } public string title { get; set; } public string url { get; set; } public List<Enclosure> enclosures { get; set; } } public class RootObject { public List<Article> articles { get; set; } public string description { get; set; } public string syndication_url { get; set; } public string title { get; set; } } 您只需将代码更改为: // Parse the response body. Blocking! return response.Content.ReadAsAsync<RootObject>().Result.articles; 显然剥去了你不需要的任何属性.只是想我会出于兴趣向他们展示. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 如何实现ASP.NET 2.0,Coldfusion 5和Classic ASP之间互操作
- asp.net-mvc-3 – 与BCrypt.net合作
- asp.net-mvc – 如何使用AngularJs显示MVC登录的用户名
- asp.net核心,角度2,PrimeNG
- asp.net – Visual Studio 2013项目使它成为武士刀项目是什
- asp.net – 在会话中存储相当大量的数据是否可以?
- Scaffolding Template on Asp.Net Core Ra
- ASP.NET 高性能分页代码
- 如何利用ASP.net IIS 7.5中的浏览器缓存
- asp.net-mvc – 在ASP.NET MVC中使用HTML表单?
推荐文章
站长推荐
- 无法将Nuget包添加到ASP.NET vNext项目
- 初识ABP vNext(6):vue+ABP实现国际化
- C#使用Linq to csv读取.csv文件数据2_处理含有非
- asp.net – OpenID Connect:验证用户ID令牌或访
- asp.net-mvc – Controller如何知道MVC中DeleteC
- asp.net-mvc-3 – 在同一视图中更新多个项目
- asp.net-mvc – 从MVC剃刀模型调用Bootstrap 3 M
- asp.net – 通过responseMode =“ExecuteURL”清
- asp.net-mvc – 从MVC版本1迁移后,ASP.NET MVC 2
- 管理页面来管理asp.net会员提供商和角色管理
热点阅读