2012-02-08 164 views
1

我正在使用的API提供了JSON有效內容。響應的一部分包含一個包含命名對象數組的屬性。我想我想將這部分反序列化到一個.Net列表中,問題是數組中的每個對象都有一個名稱,所以我無法將它作爲列表獲得。將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做到這一點的名單,但我又難倒..

+0

@Etienne de martel,你是如何格式化JSON的,我掙扎但失敗了? – Markive 2012-02-09 10:33:57

+1

像任何代碼一樣:只需用4個空格縮進每行(或者,更好的是,選擇整個代碼並按下「代碼」按鈕)。有關更多信息,請閱讀[編輯幫助](http://stackoverflow.com/editing-help)。 – 2012-02-09 14:05:34

回答

1

這種稱爲「命名對象的數組」的數據結構是衆所周知的,稱爲字典地圖。 (對於.NET,請參閱this doc。)

僅對ServiceOptions屬性使用Dictionary<string, YourServiceOptionObject>

邊注:其實物業ServiceOptions,你可能指的結構,絕不不同於CheapestQuote物業,除了一個事實,即每個關鍵而不是原始值posses這是另一個嵌套的價值字典/對象。

+1

從樹上看不到木頭..謝謝! – Markive 2012-02-09 10:37:40