【发布时间】:2017-06-11 21:55:05
【问题描述】:
执行 AJAX 请求时出现以下错误:
将值 {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);
// (...)
}
调试器说这是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
}]
ElementToUpdate 类是
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;
}
}
所以我的问题是:如何解决这个问题?如果我正确理解错误消息,那么它会说序列化数组中第 5 个 json 对象的tabID 是null。但正如你所看到的,事实并非如此。此外,ElementToUpdate.tabID 不是Int32,而是int。我错过了什么吗?
解决方案
事实上,我的 JSON 字符串包含一个元素,其 tabID 是 null。我不知何故忽略了这一点,因为首先我的 JSON 字符串在我检查时没有格式化,其次因为 [5] 表示“数组的第 6 个元素”(我声称实际上知道)。
【问题讨论】:
-
int=Int32。以防万一...C#, int or Int32? Should I care? -
@MelanciaUK 我也是这么想的。但是错误信息让我不确定...
-
我编辑了问题以扩展 JSON 字符串,这样更容易看出问题出在哪里。
标签: c# .net json json.net deserialization