RestAPI反序列化VendAPI
RestAPI Deserialization VendAPI
我正在尝试通过 restAPI 调用 Vend API。
我进行调用以执行并将数据检索到内容中,但是当我尝试通过数据 属性、项目列表
访问反序列化对象时
var 响应 = client.Execute(请求);
我在列表中得到一个条目,条目是 empty/null。
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()
我认为你的 class stock_movements
是错误的:反序列化器可能期望 class 和 list-property 而不是继承列表。
// old -> public class stock_movements : List<Stock_Movement>
public class StockMovementsResponse
{
public IList<Stock_Movement> StockMovements { get; set; }
}
然后调用:
Execute<StockMovementsResponse>(request);
我正在尝试通过 restAPI 调用 Vend API。
我进行调用以执行并将数据检索到内容中,但是当我尝试通过数据 属性、项目列表
访问反序列化对象时var 响应 = client.Execute(请求);
我在列表中得到一个条目,条目是 empty/null。
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()
我认为你的 class stock_movements
是错误的:反序列化器可能期望 class 和 list-property 而不是继承列表。
// old -> public class stock_movements : List<Stock_Movement>
public class StockMovementsResponse
{
public IList<Stock_Movement> StockMovements { get; set; }
}
然后调用:
Execute<StockMovementsResponse>(request);