网络API调用成一般类型的类存储结果 [英] Storing result of Web API call into Generic type class

查看:137
本文介绍了网络API调用成一般类型的类存储结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是这样定义这个简单的模型类:

I have this simple model class which is defined like this:

public class CountryLanguage
{
    public string Name { get; set; }
    public string ShortName { get; set; }
    public string Description { get; set; }
}

我有这个网页API,返回CountryLanguage的IEnumerable

I have this Web API which return an IEnumerable of CountryLanguage:

[HttpGet]
public IEnumerable<CountryLanguage> Get()
{        
    List<CountryLanguage> list = new List<CountryLanguage>();
    list.Add(new CountryLanguage());
    list.Add(new CountryLanguage());
    return list;
}

我有这个类,我想存储Web API调用的结果:

I have this class where I want to store the result of the Web API call:

public class ResponseResult<T>
{
    public HttpStatusCode StatusCode { get; set; }
    public string Message { get; set; }
    public T Payload { get; set; }
}

最后,这里是code调用的Web API:

And finally, here is the code calling the Web API :

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, actionToCallWithParameters);

var response = httpClient.SendAsync(request).Result;

ResponseResult<T> responseResult = new ResponseResult<T>();
responseResult.StatusCode = response.StatusCode;
responseResult.Message = response.Content.ReadAsStringAsync().Result;
responseResult.Payload = response.Content.ReadAsAsync<T>().Result;                

return responseResult;

如果网页API返回CountryLanguage对象,我得到这个存储对象到我的泛型类型属性的有效载荷没有问题的。

If the web API return a CountryLanguage object, I got no problem of storing this object into my generic type property payload.

但是,如果Web API返回一个IEnumerable,我得到这个错误:

But if the Web API returns an IEnumerable, I get this error :

无法反序列化当前的JSON数组(如[1,2,3])入式'CountryLanguage,因为类型需要一个JSON对象(如{\\名称\\:\\价值\\})正确地反序列化。要解决这个错误要么改变的JSON到一个JSON对象(如{\\名称\\:\\价值\\})或改变反序列化类型来实现一个集合接口的数组或类型(如ICollection的,IList的)像列表可以从JSON数组反序列化。 JsonArrayAttribute也可以加入到类型迫使它从JSON数组反序列化

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'CountryLanguage' because the type requires a JSON object (e.g. {\"name\":\"value\"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {\"name\":\"value\"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.

我的问题是:是否有可能正常化这code,所以我可以存储为对象或一个IEnumerable成类型T的有效载荷我的财产

My question is: Is it possible to "normalize" this code so I can store either an object or an IEnumerable into my payload property of type T?

推荐答案

好吧,这是可能的一个IEnumerable存储到泛型类型T的变量。

Ok, it is possible to store an IEnumerable into a variable of generic type T.

如果我希望返回的数据是一个对象,我需要调用的是:

If I expect the returned data to be one object, the call I need to make is:

ResponseResult<CountryLanguage> response = serviceConnector.GetData <CountryLanguage>(actionToCall);

如果我期待的IEnumerable从呼叫:

If I expect a IEnumerable from a call:

ResponseResult<IEnumerable<CountryLanguage>> response =
                serviceConnector.GetData<IEnumerable<CountryLanguage>>(actionToCall);

所以我的错误是一个事实,即调用code时,我并没有指定类型的IEnumerable

So my error was the fact that I was not specifying the IEnumerable type when calling the code.

这篇关于网络API调用成一般类型的类存储结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆