2013-03-22 57 views
0

5用於反序列化我通過Restful url獲得的JSON對象。JSON.Parse和JsonConvert.DeserializeObject失敗

下面是這兩個方法我試圖反序列化

var retObject1 = JObject.Parse(_strResponse); 
    var rootObject2 = JsonConvert.DeserializeObject<List<ProductObjLibrary>>(_strResponse); 

我的字符串響應如下

{"GetProductsResult":[{"BrandID":19081,"BrandName":"A1C NOW SELFCHECK SYSTEM","BrandNameHTML":"A1C NOW SELFCHECK SYSTEM","ClassName":"Diabetes","CleanProductURL":"a1c_now_selfcheck_system","GenericName":"blood glucose monitoring","ManufacturerName":"Bayer","ProductID":19081,"ProductName":"A1C NOW SELFCHECK SYSTEM","Rank":0},{"BrandID":19045,"BrandName":"ABILIFY","BrandNameHTML":"ABILIFY","ClassName":"Antipsychotic","CleanProductURL":"abilify","GenericName":"aripiprazole","ManufacturerName":"Bristol-Myers Squibb and Otsuka","ProductID":19045,"ProductName":"ABILIFY","Rank":0},{"BrandID":19995,"BrandName":"ABRAXANE","BrandNameHTML":"ABRAXANE","ClassName":"Oncology: Breast Cancer","CleanProductURL":"abraxane","GenericName":"paclitaxel","ManufacturerName":"Abraxis Oncology","ProductID":19995,"ProductName":"ABRAXANE","Rank":0},{"BrandID":18413,"BrandName":"ACCOLATE","BrandNameHTML":"ACCOLATE","ClassName":"Asthma\/COPD","CleanProductURL":"accolate","GenericName":"zafirlukast","ManufacturerName":"AstraZeneca Pharmaceuticals","ProductID":18413,"ProductName":"ACCOLATE","Rank":0},{"BrandID":19595,"BrandName":"ACCU-CHECK SPIRIT INSULIN PUMP","BrandNameHTML":"ACCU-CHECK SPIRIT INSULIN PUMP","ClassName":"Diabetes","CleanProductURL":"accu_check_spirit_insulin_pump","GenericName":"blood glucose monitoring","ManufacturerName":"Roche","ProductID":19595,"ProductName":"ACCU-CHECK SPIRIT INSULIN PUMP","Rank":0}]} 

後,我用第一種方法轉換此字符串retObject1我得到以下對象

{ 
    "GetProductsResult": [ 
    { 
     "BrandID": 19081, 
     "BrandName": "A1C NOW SELFCHECK SYSTEM", 
     "BrandNameHTML": "A1C NOW SELFCHECK SYSTEM", 
     "ClassName": "Diabetes", 
     "CleanProductURL": "a1c_now_selfcheck_system", 
     "GenericName": "blood glucose monitoring", 
     "ManufacturerName": "Bayer", 
     "ProductID": 19081, 
     "ProductName": "A1C NOW SELFCHECK SYSTEM", 
     "Rank": 0 
    }, 
    { 
     "BrandID": 19045, 
     "BrandName": "ABILIFY", 
     "BrandNameHTML": "ABILIFY", 
     "ClassName": "Antipsychotic", 
     "CleanProductURL": "abilify", 
     "GenericName": "aripiprazole", 
     "ManufacturerName": "Bristol-Myers Squibb and Otsuka", 
     "ProductID": 19045, 
     "ProductName": "ABILIFY", 
     "Rank": 0 
    }, 
    { 
     "BrandID": 19995, 
     "BrandName": "ABRAXANE", 
     "BrandNameHTML": "ABRAXANE", 
     "ClassName": "Oncology: Breast Cancer", 
     "CleanProductURL": "abraxane", 
     "GenericName": "paclitaxel", 
     "ManufacturerName": "Abraxis Oncology", 
     "ProductID": 19995, 
     "ProductName": "ABRAXANE", 
     "Rank": 0 
    }, 
    { 
     "BrandID": 18413, 
     "BrandName": "ACCOLATE", 
     "BrandNameHTML": "ACCOLATE", 
     "ClassName": "Asthma/COPD", 
     "CleanProductURL": "accolate", 
     "GenericName": "zafirlukast", 
     "ManufacturerName": "AstraZeneca Pharmaceuticals", 
     "ProductID": 18413, 
     "ProductName": "ACCOLATE", 
     "Rank": 0 
    } 
    ] 
} 

使用第二種方法我得到波紋管錯誤

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. 

回答

0

感謝弗雷德裏克Rofors http://codesurf.blogspot.com/2012/10/jsonconvertdeserializeobject-cannot.html

教訓:

If the json value is '[]' => declare the field as List<type> 
If the json value is '{}' => declare the field IDictionary<type, type> 

我在處理的情況下,像這樣。

var rootObject = JsonConvert.DeserializeObject<IDictionary<string, List<ProductObjLibrary>>>(_strResponse); 
         if (rootObject != null) 
          _products = rootObject.FirstOrDefault().Value; 
0

我最近遇到了同樣的問題。只是一個建議=>以便您不必擔心[]或{}中包含的json值。我爲{}中包含的json值使用了以下代碼,但請記住,我的目的是處理任何情況。

Dictionary<string, object> values = JsonConvert.DeserializeObject<Dictionary<string, object>>(response_result); 

上面的代碼只對1級或深度,爲2級的代碼將

Dictionary<string, object> values = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, object> >>(response_result); 

等多層次

Dictionary<string, Dictionary<string, Dictionary<string, Dictionary<string,**...**>> > > 

來解決這個問題多層次是非常棘手的,目前,我將會深入探討這個問題。