I am sending a API request to an endpoint and receiving a response.
I have an object RootResponse
This object is never a list and will always be a single object.
Within this object is an object called Package
and within that, an object called Activity
.
This activity object represents the information when a carrier scans a package. However sometimes there may be only 1 scan on a package in which case when I deserialize my JSON response into my RootResponse
object i get the following error
Batchtracker.Models.TrackingAPI.Response.Activity[]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type
I have tried changing the type of object Activity
to a List<Activity>
and Activity[]
but still an error. I understand that the issue is that when there is only one activity scan, it's only returning as a single object and not an array. How can i fix this issue?
Here is an example of the response activity object before deserialization when there is only one scan
"Activity": {
"ActivityLocation": {
"Address": {
"CountryCode": "US"
}
},
"Status": {
"Type": "M",
"Description": "Order Processed: Ready for UPS",
"Code": "MP"
},
"Date": "20190426",
"Time": "135618"
},
and heres the entire response for any curious
{
"TrackResponse": {
"Response": {
"ResponseStatus": {
"Code": "1",
"Description": "Success"
},
"TransactionReference": ""
},
"Shipment": {
"InquiryNumber": {
"Code": "01",
"Description": "ShipmentIdentificationNumber",
"Value": "redacted"
},
"ShipperNumber": "redacted",
"ShipmentAddress": [{
"Type": {
"Code": "01",
"Description": "Shipper Address"
},
"Address": {
"AddressLine": "redacted redacted redacted redacted",
"City": "redacted",
"StateProvinceCode": "GA",
"PostalCode": "redacted",
"CountryCode": "US"
}
}, {
"Type": {
"Code": "02",
"Description": "ShipTo Address"
},
"Address": {
"City": "redacted",
"StateProvinceCode": "redacted",
"PostalCode": "redacted",
"CountryCode": "US"
}
}],
"ShipmentWeight": {
"UnitOfMeasurement": {
"Code": "LBS"
},
"Weight": "2.00"
},
"Service": {
"Code": "003",
"Description": "UPS Ground"
},
"ReferenceNumber": {
"Code": "01",
"Value": "redacted"
},
"PickupDate": "20190426",
"DeliveryDetail": {
"Type": {
"Code": "03",
"Description": "Scheduled Delivery"
},
"Date": "20190430"
},
"Package": {
"TrackingNumber": "redacted",
"PackageServiceOption": {
"Type": {
"Code": "010",
"Description": "Hundredweight"
}
},
"Activity": {
"ActivityLocation": {
"Address": {
"CountryCode": "US"
}
},
"Status": {
"Type": "M",
"Description": "Order Processed: Ready for UPS",
"Code": "MP"
},
"Date": "20190426",
"Time": "135618"
},
"PackageWeight": {
"UnitOfMeasurement": {
"Code": "LBS"
},
"Weight": "2.00"
},
"ReferenceNumber": {
"Code": "01",
"Value": "redacted"
}
}
}
}
}