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

Posted

技术标签:

【中文标题】将 JSON 命名属性反序列化为 .Net 对象【英文标题】:Deserialize JSON named property to .Net Object 【发布时间】:2012-03-01 09:56:21 【问题描述】:

我使用的 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 来执行此操作,但我又被难住了。。

【问题讨论】:

@Etienne de martel,你是如何格式化 JSON 的,我很努力但失败了? 像任何代码一样:只需将每行缩进 4 个空格(或者,更好的是,选择整个代码并按“代码”按钮)。如需更多信息,请阅读Editing Help。 【参考方案1】:

您所说的这种“命名对象数组”是一种众所周知的数据结构,称为dictionaryma​​p。 (对于 .NET,请参阅 this doc。)

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

旁注:事实上,您可能提到的属性ServiceOptions 的结构与CheapestQuote 属性没有任何不同,除了每个而不是原始的value 拥有另一个嵌套字典/对象的值。

【讨论】:

看不到树上的木头。谢谢!

以上是关于将 JSON 命名属性反序列化为 .Net 对象的主要内容,如果未能解决你的问题,请参考以下文章

使用JSON.Net将字符串属性值反序列化为类实例

使用 Json.net 将 JSON 对象反序列化为动态对象

Json.NET:将嵌套数组反序列化为强类型对象

将 JSON 反序列化为 C# 类,其中 JSON 中的属性名称是动态的

将JSON字符串反序列化为指定的.NET对象类型

如何将具有嵌套属性的 JSON 对象反序列化为 Symfony 实体?