RestAPI 反序列化 VendAPI.
本文关键字:VendAPI 反序列化 RestAPI | 更新日期: 2023-09-27 18:32:10
我正在尝试通过restAPI调用Vend API。
我调用调用来执行并将数据检索到 Content 中,但是当我尝试通过 data 属性访问反序列化的对象时,项目列表
VAR 响应 = 客户端。执行(请求);
我在列表中得到一个条目,这些条目为空/空。
有关 API 的说明,请参阅 https://developers.vendhq.com/documentation/api/0.x/consignments.html
序列化对象的代码是:
public class stock_movements : List<Stock_Movement>
{
public stock_movements()
{
}
}
public class Stock_Movement
{
public string id {get; set;}
public string name {get; set;}
public string type {get; set;}
public DateTime date {get; set;}
public string outlet_id {get; set;}
public string supplier_id {get; set;}
public string status {get; set;}
public DateTime received_at {get; set;}
public DateTime created_at {get; set;}
public DateTime updated_at {get; set;}
public List<Product> products { get; set; }
public Stock_Movement()
{
}
}
public class Product
{
public string id {get; set;}
public string product_id {get; set;}
public string name {get; set;}
public int count {get; set;}
public int received {get; set;}
public Decimal cost {get; set;}
public DateTime created_at {get; set;}
public DateTime updated_at {get; set;}
}
调用 API 的代码为:
public stock_movements get_StockMovements()
{
var request = new RestRequest("stock_movements", Method.GET);
return Execute<stock_movements>(request);
}
private T Execute<T>(RestRequest request) where T : new()
{
var client = new RestClient(Constants.VendAPIBaseURL);
request.AddParameter("access_token", _auth.access_token, ParameterType.QueryString);
var response = client.Execute<T>(request);
if (response.ErrorException != null)
{
const string message = "Error retrieving response. Check inner details for more info.";
var vendException = new ApplicationException(message, response.ErrorException);
throw vendException;
}
return response.Data;
} // private T Execute<T>(RestRequest request) where T : new()
我认为你的类stock_movements
是错误的:反序列化程序可能需要一个具有列表属性而不是继承列表的类。
// old -> public class stock_movements : List<Stock_Movement>
public class StockMovementsResponse
{
public IList<Stock_Movement> StockMovements { get; set; }
}
然后打电话:
Execute<StockMovementsResponse>(request);