Api 映射响应对象(C#)
我正在使用一个API方法,它返回Product类型的响应,下面是响应类结构Api 映射响应对象(C#),api,Api,我正在使用一个API方法,它返回Product类型的响应,下面是响应类结构 Public class Product { public int Id; public string Name; public IList<Product> MasterProduct { get; set; } } 公共类产品 { 公共int Id; 公共字符串名称; 公共IList主产品{get;set;}
Public class Product
{
public int Id;
public string Name;
public IList<Product> MasterProduct { get; set; }
}
公共类产品
{
公共int Id;
公共字符串名称;
公共IList主产品{get;set;}
}
API结果包括产品属性和IList。由于此API不能通过windows客户端直接使用,因此我们有一个使用此API的包装器web API,为此,我们在本地API中定义了类似的产品类。我面临的问题是,当试图将外部API的姿态映射到本地API时。下面是我想做的
response = Response.Result.Select(x => new Product
{
Id=x.Id,
Name=x.Name
MasterProduct = x.MasterProduct.Cast<MasterProduct>().ToList()//tried below
}).ToList();
response=response.Result.Select(x=>newproduct
{
Id=x.Id,
Name=x.Name
MasterProduct=x.MasterProduct.Cast().ToList()//尝试如下
}).ToList();
但它失败,错误为-无法将“Api.Models.Product”类型的对象强制转换为“App.DataContracts.Product”类型
主产品由层次数据组成。我想知道我采取的方法是否正确,或者必须通过某种方法来完成。如有任何建议或帮助,将不胜感激
在搜索web时,我遇到一些代码,其中使用Microsoft.Its.Data调用serpare方法进行解析,但这是针对单个对象的,在我的示例中,我有一个列表(分层)
如果有人能指向某个linke/sampel来实现同样的功能,我将不胜感激。尝试序列化/反序列化就可以了。下面是代码 也许可以尝试序列化/反序列化
if (response.Result != null)
{
var serializedResponse = JsonConvert.SerializeObject(Response.Result, Formatting.Indented);
response = JsonConvert.DeserializeObject<List<Product>>(serializedResponse);
}).ToList();
return response;
if(response.Result!=null)
{
var serializedResponse=JsonConvert.SerializeObject(Response.Result,Formatting.Indented);
response=JsonConvert.DeserializeObject(serializedResponse);
}).ToList();
返回响应;
尝试序列化/反序列化即可。下面是代码
也许可以尝试序列化/反序列化
if (response.Result != null)
{
var serializedResponse = JsonConvert.SerializeObject(Response.Result, Formatting.Indented);
response = JsonConvert.DeserializeObject<List<Product>>(serializedResponse);
}).ToList();
return response;
if(response.Result!=null)
{
var serializedResponse=JsonConvert.SerializeObject(Response.Result,Formatting.Indented);
response=JsonConvert.DeserializeObject(serializedResponse);
}).ToList();
返回响应;
通过继承实现了相同的,也高于工作tx。通过继承实现了相同的,也高于工作tx。