I'm passing JSON code from JavaScript to C#. When I get the JSON server-side, I try to deserialize it and I get the exception "Value cannot be null. Parameter name: type". The JSON I use is:
{
"0": {
"__type": "MyProject.ResultGroupItem",
"Initiative": {
"Id": 1,
"Code": "ABC",
"Summary": "",
"Description": "",
}
},
"1": {
"__type": "MyProject.ResultGroupItem",
"Initiative": {
"Id": 2,
"Code": "XYZ",
"Summary": "",
"Description": "",
}
}
}
ResultGroupItem class:
public class ResultGroupItem
{
public Initiative Initiative { get; set; }
}
Initiative class:
public class Initiative
{
public int Id { get; set; }
public string Code { get; set; }
public string Summary { get; set; }
public string Description { get; set; }
}
Then server side when I get the JSON, I use:
List<ResultGroupItem> obj = new JavaScriptSerializer().Deserialize<List<ResultGroupItem>>(jsonString); // Exception: Value cannot be null.
Parameter name: type
I tried to move out the List<ResultGroupItem> to a new class, like:
public class Damn
{
public List<ResultGroupItem> ResultGroupItems { get; set; }
}
And then:
Damn obj = new JavaScriptSerializer().Deserialize<Damn>(jsonString); // SAME EXCEPTION
I even tried using Newtonsoft.Json library:
Damn obj = JsonConvert.DeserializeObject<Damn>(jsonString); // ResultGroupItems list inside Damn class is null
List<ResultGroupItem> obj = JsonConvert.DeserializeObject<List<ResultGroupItem>>(jsonString);
/* Exception:
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[MyProject.ResultGroupItem]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
*/
Can you advise me what to do in this case, because I don't know what to try anymore? I've read that this "__type" member in the JSON might do the problem, but also I've read that Newtonsoft.Json library handles it perfectly. Thanks for any help provided!