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.