0

Restful url を介して取得した JSON オブジェクトを逆シリアル化するための 5。

ここに私がデシリアライズしようとした2つの方法があります

  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
    }
  ]
}

2番目の方法を使用すると、次のエラーが発生します

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

私は最近同じ問題に遭遇しました。[] または {} で囲まれた 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,**...**>> > > 

この問題をマルチレベルで解決するのは非常に難しいので、現時点では、この問題に飛び込んでいきます。

于 2015-09-22T17:14:35.353 に答える
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 に答える