Newtonsoft Json Erreur lors de la conversion de la valeur {null} de type 'System.Int32'

Lors de l'exécution d'une requête AJAX, j'obtiens l'erreur suivante:

Erreur lors de la conversion de la valeur {null} de type 'System.Int32'. Chemin " [5].tabID', à la ligne 1, poste 331.

L'erreur se produit sur la deuxième ligne de mon processRequest (...)

public void ProcessRequest (HttpContext context) { 
    string strJson = new StreamReader(context.Request.InputStream).ReadToEnd();
    List<ElementToUpdate> elements = JsonConvert.DeserializeObject<List<ElementToUpdate>>(strJson);

    //(...)
}

Le débogueur dit que cette le contenu de strJson:

[{
"bmk": "132M1",
"state": "off",
"type": "motor",
"tabID": 8
}, {
"bmk": "158M1",
"state": "off",
"type": "motor",
"tabID": 8
}, {
"bmk": "194M1",
"state": "off",
"type": "motor",
"tabID": 8
}, {
"bmk": "198M1",
"state": "on",
"type": "motor",
"tabID": 8
}, {
"bmk": "202M1",
"state": "off",
"type": "motor",
"tabID": 8
}, {
"bmk": "test-m",
"state": "on",
"type": "motor",
"tabID": null
}, {
"bmk": "158M1-2",
"state": "off",
"type": "motor",
"tabID": 2
}, {
"bmk": "100M1",
"state": "on_right",
"type": "screwconveyor",
"tabID": 8
}, {
"bmk": "152M1",
"state": "on",
"type": "screwconveyor",
"tabID": 8
}, {
"bmk": "192M1",
"state": "on_left",
"type": "screwconveyor",
"tabID": 8
}, {
"bmk": "196M1",
"state": "off",
"type": "screwconveyor",
"tabID": 8
}, {
"bmk": "2000M1",
"state": "on_left",
"type": "screwconveyor",
"tabID": 8
}, {
"bmk": "74M1",
"state": "off",
"type": "screwconveyor",
"tabID": 8
}, {
"bmk": "76M1",
"state": "off",
"type": "screwconveyor",
"tabID": 8
}, {
"bmk": "80M1",
"state": "off",
"type": "screwconveyor",
"tabID": 8
}, {
"bmk": "82M1",
"state": "off",
"type": "screwconveyor",
"tabID": 8
}, {
"bmk": "86M1",
"state": "off",
"type": "screwconveyor",
"tabID": 8
}, {
"bmk": "90M1",
"state": "off",
"type": "screwconveyor",
"tabID": 8
}, {
"bmk": "94M1",
"state": "off",
"type": "screwconveyor",
"tabID": 8
}, {
"bmk": "95M1",
"state": "off",
"type": "screwconveyor",
"tabID": 8
}, {
"bmk": "96M1",
"state": "off",
"type": "screwconveyor",
"tabID": 8
}, {
"bmk": "102Y1",
"state": "closed",
"type": "ventile",
"tabID": 8
}, {
"bmk": "104Y1",
"state": "open",
"type": "ventile",
"tabID": 8
}, {
"bmk": "112Y2",
"state": "closed",
"type": "ventile",
"tabID": 8
}, {
"bmk": "114Y2",
"state": "closed",
"type": "ventile",
"tabID": 8
}, {
"bmk": "120Y1",
"state": "closed",
"type": "ventile",
"tabID": 8
}, {
"bmk": "122Y1",
"state": "closed",
"type": "ventile",
"tabID": 8
}, {
"bmk": "128Y2",
"state": "closed",
"type": "ventile",
"tabID": 8
}, {
"bmk": "146Y1_2",
"state": "closed",
"type": "ventile",
"tabID": 8
}, {
"bmk": "148Y2",
"state": "closed",
"type": "ventile",
"tabID": 8
}, {
"bmk": "156Y1",
"state": "closed",
"type": "ventile",
"tabID": 8
}, {
"bmk": "180Y1",
"state": "open",
"type": "ventile",
"tabID": 8
}, {
"bmk": "182Y1",
"state": "closed",
"type": "ventile",
"tabID": 8
}, {
"bmk": "184Y1",
"state": "open",
"type": "ventile",
"tabID": 8
}, {
"bmk": "206Y1",
"state": "open",
"type": "ventile",
"tabID": 8
}, {
"bmk": "208Y1",
"state": "closed",
"type": "ventile",
"tabID": 8
}, {
"bmk": "72Y2",
"state": "closed",
"type": "ventile",
"tabID": 8
}, {
"bmk": "78Y2",
"state": "open",
"type": "ventile",
"tabID": 8
}, {
"bmk": "84Y2",
"state": "closed",
"type": "ventile",
"tabID": 8
}, {
"bmk": "88Y2",
"state": "closed",
"type": "ventile",
"tabID": 8
}, {
"bmk": "92Y2",
"state": "closed",
"type": "ventile",
"tabID": 8
}, {
"bmk": "95_1Y1",
"state": "blocked",
"type": "ventile",
"tabID": 8
}, {
"bmk": "17H1",
"state": "on",
"type": "lamp",
"tabID": 8
}, {
"bmk": "l1",
"state": "on",
"type": "lamp",
"tabID": 8
}, {
"bmk": "17H1-2",
"state": "on",
"type": "lamp",
"tabID": 2
}, {
"bmk": "106M1",
"state": "on",
"type": "elevator",
"tabID": 8
}, {
"bmk": "154M1",
"state": "off",
"type": "elevator",
"tabID": 8
}, {
"bmk": "164M1",
"state": "off",
"type": "rotaryvalve",
"tabID": 8
}]

La classe ElementToUpdate est

public class ElementToUpdate
{
public ElementType type;
public String bmk;
public string state;
public int tabID;
public ElementToUpdate()
{
}
public ElementToUpdate(ElementType type, String bmk, string state, int tabID)
{
this.type = type;
this.bmk = bmk;
this.state = state;
this.tabID = tabID;
}
}

Donc ma question est: Comment faire pour résoudre ce problème? Si je comprends le message d'erreur correctement, alors il est dit que tabID de la 5e objet json dans le tableau sérialisé est null. Mais comme vous pouvez le voir il n'est pas. En outre ElementToUpdate.tabID n'est pas un Int32 mais un int. Ai-je raté quelque chose?

La solution

En fait, ma chaîne JSON contient un élément dont tabID était null. J'ai un peu négligé ce d'abord parce que ma chaîne JSON n'était pas formaté lorsque j'ai vérifié et, deuxièmement, parce que [5] signifie "6ème élément de la matrice" (qui je l'ai demandé à savoir, en fait).

int = Int32. Juste au cas où... C#, int ou Int32? Dois-je prendre soin?
C'est ce que je pensais, trop. Mais le message d'erreur m'a fait de doute...
J'ai édité la question d'étendre la chaîne JSON, donc il est plus facile de voir où est le problème.

OriginalL'auteur elementzero23 | 2017-01-26