2016-12-29 164 views
-1

我在處理JSON數組時遇到了一些問題,下面是我試過的。我也嘗試使用<List<jsonResponse.RootObect>並獲得相同的結果。 我使用JSON.NET無法反序列化JSON對象類型需要JSON數組(例如[1,2,3])才能正確反序列化

C#:

jsonResponse.RootObject deserializedResponse = JsonConvert.DeserializeObject<jsonResponse.RootObject>(Globals.jsonResponse); 

類:

namespace QuantumView 
{ 
    [JsonObjectAttribute] 
    class jsonResponse 
    { 
     public class TransactionReference 
     { 
      public string CustomerContext { get; set; } 
     } 

     public class Response 
     { 
      public TransactionReference TransactionReference { get; set; } 
      public string ResponseStatusCode { get; set; } 
      public string ResponseStatusDescription { get; set; } 
     } 

     public class SubscriptionStatus 
     { 
      public string Code { get; set; } 
      public string Description { get; set; } 
     } 

     public class StatusType 
     { 
      public string Code { get; set; } 
      public string Description { get; set; } 
     } 

     public class Address 
     { 
      public string AddressLine1 { get; set; } 
      public string City { get; set; } 
      public string StateProvinceCode { get; set; } 
      public string PostalCode { get; set; } 
      public string CountryCode { get; set; } 
     } 

     public class Shipper 
     { 
      public string Name { get; set; } 
      public string ShipperNumber { get; set; } 
      public Address Address { get; set; } 
     } 

     public class Address2 
     { 
      public string ConsigneeName { get; set; } 
      public string AddressLine1 { get; set; } 
      public string AddressLine2 { get; set; } 
      public string City { get; set; } 
      public string StateProvinceCode { get; set; } 
      public string PostalCode { get; set; } 
      public string CountryCode { get; set; } 
     } 

     public class ShipTo 
     { 
      public string AttentionName { get; set; } 
      public string PhoneNumber { get; set; } 
      public Address2 Address { get; set; } 
     } 

     public class ReferenceNumber 
     { 
      public string Number { get; set; } 
      public string Value { get; set; } 
     } 

     public class Service 
     { 
      public string Code { get; set; } 
     } 

     public class Activity 
     { 
      public string Date { get; set; } 
      public string Time { get; set; } 
     } 

     public class Dimensions 
     { 
      public string Length { get; set; } 
      public string Width { get; set; } 
      public string Height { get; set; } 
     } 

     public class UnitOfMeasurement 
     { 
      public string Code { get; set; } 
     } 

     public class DimensionalWeight 
     { 
      public UnitOfMeasurement UnitOfMeasurement { get; set; } 
      public string Weight { get; set; } 
     } 

     public class PackageWeight 
     { 
      public string Weight { get; set; } 
     } 

     public class ReferenceNumber2 
     { 
      public string Number { get; set; } 
      public string Value { get; set; } 
     } 

     public class PackageServiceOptions 
     { 
      public string COD { get; set; } 
     } 
     [JsonArray] 
     public class Package 
     { 
      public Activity Activity { get; set; } 
      public Dimensions Dimensions { get; set; } 
      public DimensionalWeight DimensionalWeight { get; set; } 
      public PackageWeight PackageWeight { get; set; } 
      public string TrackingNumber { get; set; } 
      public List<ReferenceNumber2> ReferenceNumber { get; set; } 
      public PackageServiceOptions PackageServiceOptions { get; set; } 
     } 

     public class BillToAccount 
     { 
      public string Option { get; set; } 
      public string Number { get; set; } 
     } 
     [JsonArray] 
     public class Manifest 
     { 
      public Shipper Shipper { get; set; } 
      public ShipTo ShipTo { get; set; } 
      public List<ReferenceNumber> ReferenceNumber { get; set; } 
      public Service Service { get; set; } 
      public string PickupDate { get; set; } 
      public string ScheduledDeliveryDate { get; set; } 
      public string ScheduledDeliveryTime { get; set; } 
      public string DocumentsOnly { get; set; } 
      public Package Package { get; set; } 
      public string ShipmentChargeType { get; set; } 
      public BillToAccount BillToAccount { get; set; } 
     } 

     public class SubscriptionFile 
     { 
      public string FileName { get; set; } 
      public StatusType StatusType { get; set; } 
      public List<Manifest> Manifest { get; set; } 
      public object Origin { get; set; } 
     } 

這是我得到的錯誤..

 [JsonArray] 
     public class SubscriptionEvents 
     { 
      public string Name { get; set; } 
      public string Number { get; set; } 
      public SubscriptionStatus SubscriptionStatus { get; set; } 

      public List<SubscriptionFile> SubscriptionFile { get; set; } 
     } 

     public class QuantumViewEvents 
     { 
      public string SubscriberID { get; set; } 
      public SubscriptionEvents SubscriptionEvents { get; set; } 
     } 

     public class QuantumViewResponse 
     { 
      public Response Response { get; set; } 
      public QuantumViewEvents QuantumViewEvents { get; set; } 
      public string Bookmark { get; set; } 
     } 

     public class RootObject 
     { 
      public QuantumViewResponse QuantumViewResponse { get; set; } 
     } 
    } 
} 
+1

查看** Related **下所有類似標題的帖子。沒有確切的json,但你有一系列的東西,並試圖反序列化只是一件事 – Plutonix

+3

你可以分享示例json – Seminda

回答

0

問題在我的班級沒有正確的屬性時,它也證明UPS api不會發送靜態類型的響應它可以每次都不同,需要一個新的類來反序列化。我還沒有找到讓班級變得靈活的方法