0

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!

George Findulov
  • 769
  • 6
  • 16
  • 1
    Isn't this because you are submitting an object? You'd rather submit an array instead. – Bellash Jul 22 '15 at 07:21
  • 2
    Your JSON structure implies that it's actually a `Dictionary` because every item is associated with a key ("0", "1" etc). – haim770 Jul 22 '15 at 07:24
  • 1
    It's because there is no array in your JSON -> [ ] – Robert Jul 22 '15 at 07:25
  • Also, if the JSON is being passed to some controller action, you shouldn't handle the deserialization yourself, it's the task of the `ModelBinder` (or `MediaFormatter` for API controller) to create it for you. – haim770 Jul 22 '15 at 07:28
  • Thank you guys for your help, I used jQuery to convert my JSON object to an array before sending it to the server, using: var arr = $j.map(jsonString, function (el) { return el; }); Then the deserialization gets processed correctly! @haim770 - you are correct, but I'm using Web Forms :) – George Findulov Jul 22 '15 at 07:33

0 Answers0