0

5 用于反序列化我通过 Restful url 获得的 JSON 对象。

这是我尝试反序列化的两种方法

  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.
4

2 回答 2

0

感谢 Fredrik 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;
于 2013-03-22T20:14:17.040 回答
0

我最近遇到了同样的问题。只是一个建议 => 这样您就不必担心 [] 或 {} 中包含的 json 值。我将以下代码用于包含在 {} 中的 json 值,但请记住,我的目的是处理任何情况。

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

以上代码仅适用于第一级或深度,第二级代码将是

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

等等多级

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

为多层次解决这个问题非常棘手,目前,我将深入研究这个问题。

于 2015-09-22T17:14:35.353 回答