当响应对象可以是列表或单个对象时,如何反序列化JSON

时间:2019-04-29 19:22:06

标签: c# deserialization

我正在向端点发送API请求并收到响应。

我有一个对象RootResponse,该对象永远不会是列表,并且永远是单个对象。

在此对象中有一个名为Package的对象,在其中是一个名为Activity的对象。

此活动对象表示承运人扫描包裹时的信息。但是,有时在包装上可能只有1次扫描,在这种情况下,当我将JSON响应反序列化为RootResponse对象时,会出现以下错误

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

我尝试将对象Activity的类型更改为List<Activity>Activity[],但仍然出错。我知道问题在于,只有一个活动扫描时,它只会作为单个对象而不是数组返回。我该如何解决这个问题?

这是反序列化之前只有一次扫描的响应活动对象的示例

"Activity": {
  "ActivityLocation": {
    "Address": {
      "CountryCode": "US"
    }
  },
  "Status": {
    "Type": "M",
    "Description": "Order Processed: Ready for UPS",
    "Code": "MP"
  },
  "Date": "20190426",
  "Time": "135618"
},

并对任何好奇的情况进行完整的答复

{
  "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"
        }
      }
    }
  }
}

0 个答案:

没有答案