The API I'm using provides a JSON payload. Part of the response has a property which contains an array of named objects. I think I want to deserialize this part into a .Net List, the problem is each object in the array is prepended with a name so I can't get it as a list.
我正在使用的API提供了JSON有效负载。响应的一部分有一个属性,其中包含一组命名对象。我想我想将这部分反序列化为.Net列表,问题是数组中的每个对象前面都有一个名称,所以我不能把它作为一个列表。
Here's the payload:
这是有效载荷:
{
"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"
}
}
}
I want a list of 'ServiceOptions'.. I would be happy to use Json.Net to do this, but again I'm stumped..
我想要一个'ServiceOptions'列表..我很乐意使用Json.Net来做这件事,但我又难过了......
1 个解决方案
#1
1
Such an "array of named objects" as you called it is a well-know data structure and is called dictionary or map. (For .NET see this doc.)
你所称的这种“命名对象数组”是一个众所周知的数据结构,称为字典或映射。 (对于.NET,请参阅此文档。)
Just use Dictionary<string, YourServiceOptionObject>
for ServiceOptions
property.
只需对ServiceOptions属性使用Dictionary
Side note: in fact the structure of the property ServiceOptions
, which you probably refer to, differs in no way from CheapestQuote
property, apart from the fact that each key instead of primitive value posses the value which is another nested dictionary/object.
旁注:实际上,您可能引用的属性ServiceOptions的结构与CheapestQuote属性完全不同,除了每个键而不是原始值具有另一个嵌套字典/对象的值。
#1
1
Such an "array of named objects" as you called it is a well-know data structure and is called dictionary or map. (For .NET see this doc.)
你所称的这种“命名对象数组”是一个众所周知的数据结构,称为字典或映射。 (对于.NET,请参阅此文档。)
Just use Dictionary<string, YourServiceOptionObject>
for ServiceOptions
property.
只需对ServiceOptions属性使用Dictionary
Side note: in fact the structure of the property ServiceOptions
, which you probably refer to, differs in no way from CheapestQuote
property, apart from the fact that each key instead of primitive value posses the value which is another nested dictionary/object.
旁注:实际上,您可能引用的属性ServiceOptions的结构与CheapestQuote属性完全不同,除了每个键而不是原始值具有另一个嵌套字典/对象的值。