When performing an AJAX request I am getting the following error:

将值{null}转换为类型"System"时出错.Int32'.路径"[5].tabID',第1行,位置331.

错误发生在我的processRequest (...)的第二行

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

    // (...)
}

The debugger says that this the content of 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
}]

The class ElementToUpdate is

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;
}
}

So my question is: How to resolve this issue? If I understand the error message correctly, then it says that tabID of the 5th json object in the serialized array is null. But as you can see it isn't. Moreover ElementToUpdate.tabID isn't an Int32 but an int. Did I miss something?

The solution

事实上,我的JSON字符串包含一个tabIDnull的元素.我不知何故忽略了这一点,因为首先,当我判断时,我的JSON字符串没有格式化;其次,因为[5]表示"数组的第六个元素"(我声称实际上知道它).

推荐答案

使您的TABID可为空:

public int? tabID;

Value null could not be deserialized to integer. That's your 6-th part of json (or 5-th starting with 0):

{\"bmk\":\"test-m\",\"state\":\"on\",\"type\":\"motor\",\"tabID\":null}

You could see, that tabID is null there.

Json相关问答推荐

使用SQL查询从SON中查找第n个密钥对值

JOLT转换过滤出特定值/对象

在T—SQL中将STR_AGG与JSON_ARRAY结合起来

JSON API返回多个数组,需要帮助拼合数据以存储在SQL Server数据库表中

JOLT转换以根据条件删除json对象

jq 对特定键进行过滤并将值整理到单个 csv 单元格中

报告重复的对象键

展平多个数组以保持顺序

从包含 JSON 对象序列的文件中获取第一个 JSON 对象

使用 JQ 获取 JSON 中的替代元素(输出:JSON 对象)

jq: Select 何时来自另一个数组的值与此 json 中的值匹配

派生类的属性没有得到价值

在Flutter 中将 map 的 Json 转换为 list

如果 jq 数组中的字符串是对象或字符串,则获取值

C# 合并 2 个几乎相同的 JSON 对象

读取 HttpwebResponse json 响应,C#

jQuery循环.each()JSON键/值不起作用

如何将有向无环图 (DAG) 存储为 JSON?

为不同类型的项目数组正确的 JSON Schema

使用 JSON.Net 的 C# 到 JSON 序列化