将JSON命名属性反序列化为.Net Object

时间:2012-02-08 22:04:21

标签: .net json jsonserializer

我使用的API提供了JSON有效负载。响应的一部分有一个属性,其中包含一组命名对象。我想我想将这部分反序列化为.Net列表,问题是数组中的每个对象都附加了一个名称,所以我无法将其作为列表。

这是有效载荷:

{
  "ShipAction": "GetQuote",
  "Status": "Success",
  "RequestDate": "2012-02-08 21:18:19",
  "Username": "tpi",
  "QuoteID": "6M15AQOrCl059x",
  "QuoteExpires": "2012-02-22 21:18:20",
  "NoServices": 8,
  "DeliveryCountry": "France",
  "CheapestQuote": {
    "SubTotal": "15.96",
    "VatTotal": "3.19",
    "TotalPrice": "19.15"
  },
  "ServiceOptions": {
    "PF_INT": {
      "OptionID": "6183545",
      "Carrier": "PF_INT",
      "ServiceName": "Euro Priority",
      "AlternateName": false,
      "ServiceID": "PF_INT",
      "SubjectToVat": "Y",
      "SubTotal": "15.96",
      "VatTotal": "3.19",
      "TotalPrice": "19.15"
    },
    "FEDEX_ECON": {
      "OptionID": "6183549",
      "Carrier": "FEDEX",
      "ServiceName": "Economy Delivery",
      "AlternateName": false,
      "ServiceID": "FEDEX_ECON",
      "SubjectToVat": "Y",
      "SubTotal": "18.56",
      "VatTotal": "3.71",
      "TotalPrice": "22.27"
    },
    "FEDEX_PRI": {
      "OptionID": "6183548",
      "Carrier": "FEDEX",
      "ServiceName": "International Express",
      "AlternateName": false,
      "ServiceID": "FEDEX_PRI",
      "SubjectToVat": "Y",
      "SubTotal": "20.07",
      "VatTotal": "4.01",
      "TotalPrice": "24.08"
    },
    "TNT_EXPRESS": {
      "OptionID": "6183550",
      "Carrier": "TNT Export",
      "ServiceName": "TNT International Express",
      "AlternateName": false,
      "ServiceID": "TNT_EXPRESS",
      "SubjectToVat": "Y",
      "SubTotal": "20.73",
      "VatTotal": "4.15",
      "TotalPrice": "24.88"
    },
    "DHL_EURO": {
      "OptionID": "6183546",
      "Carrier": "DHL",
      "ServiceName": "DHL Euro Plus",
      "AlternateName": false,
      "ServiceID": "DHL_EURO",
      "SubjectToVat": "Y",
      "SubTotal": "23.10",
      "VatTotal": "4.62",
      "TotalPrice": "27.72"
    },
    "TNT_ECONOMY": {
      "OptionID": "6183551",
      "Carrier": "TNT Export",
      "ServiceName": "TNT International Economy Express",
      "AlternateName": false,
      "ServiceID": "TNT_ECONOMY",
      "SubjectToVat": "Y",
      "SubTotal": "24.14",
      "VatTotal": "4.83",
      "TotalPrice": "28.97"
    },
    "DHL_INT": {
      "OptionID": "6183547",
      "Carrier": "DHL TD",
      "ServiceName": "Time Definite",
      "AlternateName": false,
      "ServiceID": "DHL_INT",
      "SubjectToVat": "Y",
      "SubTotal": "30.79",
      "VatTotal": "6.16",
      "TotalPrice": "36.95"
    },
    "PALLETWAYS_EURO_QUAR": {
      "OptionID": "6183552",
      "Carrier": "PALLETWAYS_EURO",
      "ServiceName": "Quarter Pallet Delivery",
      "AlternateName": false,
      "ServiceID": "PALLETWAYS_EURO_QUAR",
      "SubjectToVat": "Y",
      "SubTotal": "152.67",
      "VatTotal": "30.53",
      "TotalPrice": "183.20"
    }
  }
}

我想要一个' ServiceOptions的列表' ..我很乐意使用Json.Net来做到这一点,但我又一次难倒......

1 个答案:

答案 0 :(得分:1)

您所谓的“命名对象数组”是一个众所周知的数据结构,称为字典映射。 (对于.NET,请参阅this doc。)

只需将Dictionary<string, YourServiceOptionObject>用于ServiceOptions属性。

附注:实际上,您可能引用的属性ServiceOptions的结构与CheapestQuote属性的区别在于,除了每个而不是原始值拥有另一个嵌套字典/对象的值。