5用于反序列化我的JSON对象,我得到了Restful url。
以下是我尝试反序列化的两种方法
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 :(得分: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;
答案 1 :(得分:0)
我最近遇到了同样的问题。只是一个建议=&gt;这样你就不必担心[]或{}中包含的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,**...**>> > >
为多层次解决这个问题是非常棘手的,目前,我将深入研究这个问题。