使用RestSharp从API返回JSON数据



我在我的MVC项目中使用RestSharp。我有这两个类作为Models用于API数据。

public class ApiResponseON
{
public Data Data { get; set; }
}
public class Data
{
public string? comments { get; set; }
}
我点击API返回这样的数据
public async Task<List<ApiResponseON>> GetJson(string on
{
RestRequest restRequest = new RestRequest($"/search?filter.on={on}&filter.status=All");
var data = await _restClient.GetAsync<List<ApiResponseON>>(restRequest);
return data;
}

,但我的问题是,我得到这个错误在return data;

系统。InvalidCastException: '无法转换类型的对象。"RestSharp。JsonArray'来键入system . string,‘System.Collections.Generic.IDictionary 2 [System.Object]"。">

我需要在代码中做什么修改才能使它成功执行?

原始Json

{
"TotalRecords":1,
"Skip":0,
"Take":100,
"ReturnCode":"Success",
"Data":[
{
"OrderUser":{
"Email":"",
"FullName":"",
"Username":"",
"FirstName":"",
"LastName":"",
"ExternalId":"",
"Custom01":"",
"Custom02":"",
"Custom03":"",
"UserGroupName":""
},
"OrderNumber":"",
"CurrencyCode":"USD",
"OrderTag":null,
"PoNumber":"",
"Comments":"4321",
"CompanyName":"",
"CompanyGuid":"00000000-0000-0000-0000-000000000000",
"CompanyId":2295,
"OrderStatus":"Active",
"AccountingUnit":null,
"EmailAddress":"",
"ShippinndHandling":0,
"Tax":0,
"Discount":0,
"OrderDate":"2017-10-28T12:16:30.453",
"ClosedDate":null,
"DesiredShipDate":"2017-11-03T23:59:59",
"Carrier":"Flat Rate",
"Shippinddress":{
"Company":"",
"Name":"",
"AddressLine1":"",
"AddressLine2":"",
"AddressLine3":"",
"City":"",
"State":"",
"Country":"US",
"ZipCode":"",
"PhoneNumber":"",
"ExternalId":null,
"Email":"",
"ShipmentCode":null
},
"Billinddress":{
"Company":"",
"Name":"",
"AddressLine1":"",
"AddressLine2":"",
"AddressLine3":null,
"City":"",
"State":"",
"Country":"US",
"ZipCode":"",
"PhoneNumber":"",
"ExternalId":null,
"Email":null,
"ShipmentCode":null
},
"Lines":[
{
"LineNumber":1,
"LineId":"",
"ApprovalReason":null,
"Price":0,
"DirectMailService":null,
"Status":"Released",
"EstimatedShipDate":"2017-11-03T23:59:59",
"Sku":"",
"PrintFileUrl":"",
"PrintFilename":"",
"ListFilUrl":null,
"ListFilename":"",
"Quantity":1,
"FileUrl":null,
"Thumbnail":null,
"UnitPrice":0,
"Part":{
"Id":1073225,
"Name":"",
"UnitCost":0,
"MinQuantity":0,
"MinQuantity2":0,
"MaxQuantity":99999,
"DaysOfStockMin":null,
"DateAvailable":"2017-10-18T00:00:00",
"Comments":"",
"CompanyExternalId1":"",
"CompanyExternalId2":"",
"HasListVariables":false,
"SiteId":127,
"CompanyId":2295,
"IsKit":false,
"IsLimitPartOrderQuantity":false,
"IsEdeliveryOnly":false,
"IsPersonalizeable":false,
"VendorName":null,
"VendorId":null,
"ShipmentGroupId":null,
"BoxSizeId":null,
"IsConfigurable":false,
"IsFixedAsset":false,
"Options":{
"PartOptionId":1222272,
"DateUpdated":"2017-10-18T10:30:43.503",
"IsCorporateListAllowed":false,
"IsEnforceMasterConfig":false,
"IsFulfillChargeExempt":false,
"IsMailingListRequired":false,
"IsPurchaseListAllowed":false,
"IsTaxExempt":false,
"IsUploadListAllowed":false,
"IsUploadListOnly":false,
"PartId":1073225,
"IsShippingExempt":false,
"ComponentItemType":null,
"ComponentMinCount":null,
"ComponentMaxCount":null,
"AllowNetiveDemand":null,
"TaxCode":null,
"UseListQtyForKitParts":false,
"IsShowBackorderAlert":null,
"ChiliDocumentId":null,
"IsFixedAsset":false,
"FixedAssetFrontBufferDays":null,
"FixedAssetBackBufferDays":null,
"IsBulkProofing":false,
"AllowCopyCustomization":null,
"IsForcedDynamicDatasource":false,
"AllowFacebook":false,
"AllowFacebookCaption":false,
"MailingListDeduplicationType":null
},
"UnitOfMeasure":null,
"ThumbnailFile":"image-not-found.png",
"Categories":[

],
"BoxSize":null,
"SourcingZip":null,
"SourcingId":null,
"IsShippingExempt":false,
"ProductionQueue":null,
"IsBulkProofing":false,
"IsOneToOneMailing":false,
"Rating":0,
"ReviewsCount":0
},
"VendorPart":null,
"Custom01":"",
"Custom02":"",
"Custom03":"",
"VendorId":null,
"Discount":0,
"DiscountName":null,
"DiscountKey":null,
"Weight":0,
"WeightedEstimatedShipping":0,
"EDeliveryUrl":"",
"FulfillCharge":0,
"IsEDelivery":false,
"IsTaxExempt":false,
"IsFullfillExempt":false,
"IsCustomized":false,
"DeliveryMethod":null,
"AccountingUnit":null,
"AccountingUnitDetails":null,
"ProofName":null,
"ProofInitial":null,
"ProofDate":null,
"ProducingPartner":null,
"Attributes":[

],
"PrintInfo":{
"NumberOfPages":0,
"FinishedTrimWidth":0,
"FinishedTrimHeight":0,
"Duplex":null,
"Bleed":null,
"Folds":null,
"PaperStock":null,
"PressSheetSize":null,
"NumberOut":null,
"BindarySpecifications":null,
"FlatSize":null,
"ProductionPress":null
},
"ShipmentLines":[

],
"RawMaterials":[

],
"CoopBucketName":null,
"ProofComments":"",
"IsFixedAsset":"N",
"RequestedArrivalDate":null,
"ExpectedReturnDate":null
}
],
"IsEDeliveryOnly":false,
"ShowInvoiceEmail":false,
"InvoiceEmail":"",
"InvoiceFee":0,
"Total":0,
"SubTotal":0,
"TotalMailingCost":0,
"IsExpedite":false,
"PartShippingCost":null,
"OrderFiles":[

],
"ThirdPartyAccountNumber":"",
"SignatureRequired":false
}
],
"Message":null,
"ErrorGuid":null
}

注意这个模型是由众多JSON到c#转换器中的一个获得的,你可以在Visual Studio中完成,或者有免费的网站。

给定

public class Rootobject
{
public int TotalRecords { get; set; }
public int Skip { get; set; }
public int Take { get; set; }
public string ReturnCode { get; set; }
public Datum[] Data { get; set; }
public object Message { get; set; }
public object ErrorGuid { get; set; }
}
public class Datum
{
public Orderuser OrderUser { get; set; }
public string OrderNumber { get; set; }
public string CurrencyCode { get; set; }
public object OrderTag { get; set; }
public string PoNumber { get; set; }
public string Comments { get; set; }
public string CompanyName { get; set; }
public string CompanyGuid { get; set; }
public int CompanyId { get; set; }
public string OrderStatus { get; set; }
public object AccountingUnit { get; set; }
public string EmailAddress { get; set; }
public int ShippinndHandling { get; set; }
public int Tax { get; set; }
public int Discount { get; set; }
public DateTime OrderDate { get; set; }
public object ClosedDate { get; set; }
public DateTime DesiredShipDate { get; set; }
public string Carrier { get; set; }
public Shippinddress Shippinddress { get; set; }
public Billinddress Billinddress { get; set; }
public Line[] Lines { get; set; }
public bool IsEDeliveryOnly { get; set; }
public bool ShowInvoiceEmail { get; set; }
public string InvoiceEmail { get; set; }
public int InvoiceFee { get; set; }
public int Total { get; set; }
public int SubTotal { get; set; }
public int TotalMailingCost { get; set; }
public bool IsExpedite { get; set; }
public object PartShippingCost { get; set; }
public object[] OrderFiles { get; set; }
public string ThirdPartyAccountNumber { get; set; }
public bool SignatureRequired { get; set; }
}
public class Orderuser
{
public string Email { get; set; }
public string FullName { get; set; }
public string Username { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string ExternalId { get; set; }
public string Custom01 { get; set; }
public string Custom02 { get; set; }
public string Custom03 { get; set; }
public string UserGroupName { get; set; }
}
public class Shippinddress
{
public string Company { get; set; }
public string Name { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string AddressLine3 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Country { get; set; }
public string ZipCode { get; set; }
public string PhoneNumber { get; set; }
public object ExternalId { get; set; }
public string Email { get; set; }
public object ShipmentCode { get; set; }
}
public class Billinddress
{
public string Company { get; set; }
public string Name { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public object AddressLine3 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Country { get; set; }
public string ZipCode { get; set; }
public string PhoneNumber { get; set; }
public object ExternalId { get; set; }
public object Email { get; set; }
public object ShipmentCode { get; set; }
}
public class Line
{
public int LineNumber { get; set; }
public string LineId { get; set; }
public object ApprovalReason { get; set; }
public int Price { get; set; }
public object DirectMailService { get; set; }
public string Status { get; set; }
public DateTime EstimatedShipDate { get; set; }
public string Sku { get; set; }
public string PrintFileUrl { get; set; }
public string PrintFilename { get; set; }
public object ListFilUrl { get; set; }
public string ListFilename { get; set; }
public int Quantity { get; set; }
public object FileUrl { get; set; }
public object Thumbnail { get; set; }
public int UnitPrice { get; set; }
public Part Part { get; set; }
public object VendorPart { get; set; }
public string Custom01 { get; set; }
public string Custom02 { get; set; }
public string Custom03 { get; set; }
public object VendorId { get; set; }
public int Discount { get; set; }
public object DiscountName { get; set; }
public object DiscountKey { get; set; }
public int Weight { get; set; }
public int WeightedEstimatedShipping { get; set; }
public string EDeliveryUrl { get; set; }
public int FulfillCharge { get; set; }
public bool IsEDelivery { get; set; }
public bool IsTaxExempt { get; set; }
public bool IsFullfillExempt { get; set; }
public bool IsCustomized { get; set; }
public object DeliveryMethod { get; set; }
public object AccountingUnit { get; set; }
public object AccountingUnitDetails { get; set; }
public object ProofName { get; set; }
public object ProofInitial { get; set; }
public object ProofDate { get; set; }
public object ProducingPartner { get; set; }
public object[] Attributes { get; set; }
public Printinfo PrintInfo { get; set; }
public object[] ShipmentLines { get; set; }
public object[] RawMaterials { get; set; }
public object CoopBucketName { get; set; }
public string ProofComments { get; set; }
public string IsFixedAsset { get; set; }
public object RequestedArrivalDate { get; set; }
public object ExpectedReturnDate { get; set; }
}
public class Part
{
public int Id { get; set; }
public string Name { get; set; }
public int UnitCost { get; set; }
public int MinQuantity { get; set; }
public int MinQuantity2 { get; set; }
public int MaxQuantity { get; set; }
public object DaysOfStockMin { get; set; }
public DateTime DateAvailable { get; set; }
public string Comments { get; set; }
public string CompanyExternalId1 { get; set; }
public string CompanyExternalId2 { get; set; }
public bool HasListVariables { get; set; }
public int SiteId { get; set; }
public int CompanyId { get; set; }
public bool IsKit { get; set; }
public bool IsLimitPartOrderQuantity { get; set; }
public bool IsEdeliveryOnly { get; set; }
public bool IsPersonalizeable { get; set; }
public object VendorName { get; set; }
public object VendorId { get; set; }
public object ShipmentGroupId { get; set; }
public object BoxSizeId { get; set; }
public bool IsConfigurable { get; set; }
public bool IsFixedAsset { get; set; }
public Options Options { get; set; }
public object UnitOfMeasure { get; set; }
public string ThumbnailFile { get; set; }
public object[] Categories { get; set; }
public object BoxSize { get; set; }
public object SourcingZip { get; set; }
public object SourcingId { get; set; }
public bool IsShippingExempt { get; set; }
public object ProductionQueue { get; set; }
public bool IsBulkProofing { get; set; }
public bool IsOneToOneMailing { get; set; }
public int Rating { get; set; }
public int ReviewsCount { get; set; }
}
public class Options
{
public int PartOptionId { get; set; }
public DateTime DateUpdated { get; set; }
public bool IsCorporateListAllowed { get; set; }
public bool IsEnforceMasterConfig { get; set; }
public bool IsFulfillChargeExempt { get; set; }
public bool IsMailingListRequired { get; set; }
public bool IsPurchaseListAllowed { get; set; }
public bool IsTaxExempt { get; set; }
public bool IsUploadListAllowed { get; set; }
public bool IsUploadListOnly { get; set; }
public int PartId { get; set; }
public bool IsShippingExempt { get; set; }
public object ComponentItemType { get; set; }
public object ComponentMinCount { get; set; }
public object ComponentMaxCount { get; set; }
public object AllowNetiveDemand { get; set; }
public object TaxCode { get; set; }
public bool UseListQtyForKitParts { get; set; }
public object IsShowBackorderAlert { get; set; }
public object ChiliDocumentId { get; set; }
public bool IsFixedAsset { get; set; }
public object FixedAssetFrontBufferDays { get; set; }
public object FixedAssetBackBufferDays { get; set; }
public bool IsBulkProofing { get; set; }
public object AllowCopyCustomization { get; set; }
public bool IsForcedDynamicDatasource { get; set; }
public bool AllowFacebook { get; set; }
public bool AllowFacebookCaption { get; set; }
public object MailingListDeduplicationType { get; set; }
}
public class Printinfo
{
public int NumberOfPages { get; set; }
public int FinishedTrimWidth { get; set; }
public int FinishedTrimHeight { get; set; }
public object Duplex { get; set; }
public object Bleed { get; set; }
public object Folds { get; set; }
public object PaperStock { get; set; }
public object PressSheetSize { get; set; }
public object NumberOut { get; set; }
public object BindarySpecifications { get; set; }
public object FlatSize { get; set; }
public object ProductionPress { get; set; }
}

使用

var result = _restClient.GetAsync<Rootobject>(restRequest);

一旦你有了你的结果,你可以按照你喜欢的方式修剪它,我把它留给你,这只是为了解决序列化错误。

最新更新