无法反序列化JSON对象类型需要JSON数组(例如[1,2,3])才能正确反序列化

时间:2016-12-29 22:53:03

标签: c# json json.net

我在处理JSON数组时遇到了一些问题,这就是我尝试过的。我也试过使用<List<jsonResponse.RootObect>并获得相同的结果。 我正在使用JSON.NET

C#:

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

CLASS:

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 个答案:

答案 0 :(得分:0)

问题在于我的班级没有正确的属性,事实证明UPS api不发送静态类型响应,每次都可能不同......需要新的类反序列化。我还没有找到让课程灵活的方法