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.