意外结果,在 ASP.Net 中解析 JSON 对象的数组
Unexpected results, Parse Array of JSON Objects in ASP.Net
我正在尝试学习MVC ASP.Net 我想将JSON对象数组发布到服务器并将其发送回客户端。但我得到了奇怪的结果。一切都很好,没有添加第四个变量(bool Required
)。例如,我还尝试在发布 JSON 的 JavaScript 部分中将 true
更改为"true"
,但我从服务器返回了相同的奇怪结果。POST的结果在使用JavaScript和POSTMAN时都是相同的,我认为问题出在服务器端代码上。
任何帮助都会很棒。
这是我的控制器代码:
public ActionResult GetResult()
{
List<SurveyDetails> tableList = new List<SurveyDetails>();
tableList.Add(new SurveyDetails { Id = 500, Question = "where are you from", Answer = 2, Required = true });
tableList.Add(new SurveyDetails { Id = 501, Question = "how old are you", Answer = 1, Required = false });
tableList.Add(new SurveyDetails { Id = 502, Question = "what is your first car", Answer = 2, Required = false });
tableList.Add(new SurveyDetails { Id = 503, Question = "do you have kids", Answer = 1, Required = true });
return Json(tableList, JsonRequestBehavior.AllowGet);
}
[HttpPost]
public ActionResult GetResult(List<Table> list)
{
return Json(list);
}
这是我的对象声明:
namespace CapstoneProject.Models
{
public class SurveyDetails
{
public int Id { get; set; }
public string Question { get; set; }
public int Answer { get; set; }
public bool Required { get; set; }
}
}
这是我从服务器(GET请求)得到的:
[
{
"Id": 500,
"Question": "where are you from",
"Answer": 2,
"Required": true
},
{
"Id": 501,
"Question": "how old are you",
"Answer": 1,
"Required": false
},
{
"Id": 502,
"Question": "what is your first car",
"Answer": 2,
"Required": false
},
{
"Id": 503,
"Question": "do you have kids",
"Answer": 1,
"Required": true
}
]
在这里,我在发布我在 GET 请求中收到的内容后从服务器返回的奇怪结果:
[
{
"BackImageUrl": "",
"Caption": "",
"CaptionAlign": 0,
"CellPadding": -1,
"CellSpacing": -1,
"GridLines": 0,
"HorizontalAlign": 0,
"SupportsDisabledAttribute": false,
"Rows": [],
"AccessKey": "",
"Attributes": {
"Keys": [],
"Count": 0,
"CssStyle": {
"Keys": [],
"Count": 0,
"Value": null
}
},
"BackColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"BorderColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"BorderWidth": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"BorderStyle": 0,
"ControlStyle": {
"BackImageUrl": "",
"CellPadding": -1,
"CellSpacing": -1,
"GridLines": 0,
"HorizontalAlign": 0,
"BackColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"BorderColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"BorderWidth": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"BorderStyle": 0,
"CssClass": "",
"Font": {
"Bold": false,
"Italic": false,
"Name": "",
"Names": [],
"Overline": false,
"Size": {
"IsEmpty": true,
"Type": 0,
"Unit": {
"IsEmpty": true,
"Type": 1,
"Value": 0
}
},
"Strikeout": false,
"Underline": false
},
"ForeColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"Height": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"IsEmpty": true,
"RegisteredCssClass": "",
"Width": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"Site": null,
"Container": null
},
"ControlStyleCreated": true,
"CssClass": "",
"Style": {
"Keys": [],
"Count": 0,
"Value": null
},
"Enabled": true,
"EnableTheming": true,
"Font": {
"Bold": false,
"Italic": false,
"Name": "",
"Names": [],
"Overline": false,
"Size": {
"IsEmpty": true,
"Type": 0,
"Unit": {
"IsEmpty": true,
"Type": 1,
"Value": 0
}
},
"Strikeout": false,
"Underline": false
},
"ForeColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"HasAttributes": false,
"Height": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"SkinID": "",
"TabIndex": 0,
"ToolTip": "",
"Width": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"ClientIDMode": 0,
"ClientID": "500",
"ID": "500",
"EnableViewState": true,
"ViewStateMode": 0,
"NamingContainer": null,
"BindingContainer": null,
"DataItemContainer": null,
"DataKeysContainer": null,
"Page": null,
"RenderingCompatibility": {
"Major": 4,
"Minor": 5,
"Build": -1,
"Revision": -1,
"MajorRevision": -1,
"MinorRevision": -1
},
"TemplateControl": null,
"Parent": null,
"TemplateSourceDirectory": "/Survey",
"AppRelativeTemplateSourceDirectory": "~/Survey/",
"Site": null,
"Visible": true,
"UniqueID": "500",
"Controls": [],
"ValidateRequestMode": 0
},
{
"BackImageUrl": "",
"Caption": "",
"CaptionAlign": 0,
"CellPadding": -1,
"CellSpacing": -1,
"GridLines": 0,
"HorizontalAlign": 0,
"SupportsDisabledAttribute": false,
"Rows": [],
"AccessKey": "",
"Attributes": {
"Keys": [],
"Count": 0,
"CssStyle": {
"Keys": [],
"Count": 0,
"Value": null
}
},
"BackColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"BorderColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"BorderWidth": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"BorderStyle": 0,
"ControlStyle": {
"BackImageUrl": "",
"CellPadding": -1,
"CellSpacing": -1,
"GridLines": 0,
"HorizontalAlign": 0,
"BackColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"BorderColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"BorderWidth": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"BorderStyle": 0,
"CssClass": "",
"Font": {
"Bold": false,
"Italic": false,
"Name": "",
"Names": [],
"Overline": false,
"Size": {
"IsEmpty": true,
"Type": 0,
"Unit": {
"IsEmpty": true,
"Type": 1,
"Value": 0
}
},
"Strikeout": false,
"Underline": false
},
"ForeColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"Height": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"IsEmpty": true,
"RegisteredCssClass": "",
"Width": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"Site": null,
"Container": null
},
"ControlStyleCreated": true,
"CssClass": "",
"Style": {
"Keys": [],
"Count": 0,
"Value": null
},
"Enabled": true,
"EnableTheming": true,
"Font": {
"Bold": false,
"Italic": false,
"Name": "",
"Names": [],
"Overline": false,
"Size": {
"IsEmpty": true,
"Type": 0,
"Unit": {
"IsEmpty": true,
"Type": 1,
"Value": 0
}
},
"Strikeout": false,
"Underline": false
},
"ForeColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"HasAttributes": false,
"Height": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"SkinID": "",
"TabIndex": 0,
"ToolTip": "",
"Width": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"ClientIDMode": 0,
"ClientID": "501",
"ID": "501",
"EnableViewState": true,
"ViewStateMode": 0,
"NamingContainer": null,
"BindingContainer": null,
"DataItemContainer": null,
"DataKeysContainer": null,
"Page": null,
"RenderingCompatibility": {
"Major": 4,
"Minor": 5,
"Build": -1,
"Revision": -1,
"MajorRevision": -1,
"MinorRevision": -1
},
"TemplateControl": null,
"Parent": null,
"TemplateSourceDirectory": "/Survey",
"AppRelativeTemplateSourceDirectory": "~/Survey/",
"Site": null,
"Visible": true,
"UniqueID": "501",
"Controls": [],
"ValidateRequestMode": 0
},
{
"BackImageUrl": "",
"Caption": "",
"CaptionAlign": 0,
"CellPadding": -1,
"CellSpacing": -1,
"GridLines": 0,
"HorizontalAlign": 0,
"SupportsDisabledAttribute": false,
"Rows": [],
"AccessKey": "",
"Attributes": {
"Keys": [],
"Count": 0,
"CssStyle": {
"Keys": [],
"Count": 0,
"Value": null
}
},
"BackColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"BorderColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"BorderWidth": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"BorderStyle": 0,
"ControlStyle": {
"BackImageUrl": "",
"CellPadding": -1,
"CellSpacing": -1,
"GridLines": 0,
"HorizontalAlign": 0,
"BackColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"BorderColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"BorderWidth": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"BorderStyle": 0,
"CssClass": "",
"Font": {
"Bold": false,
"Italic": false,
"Name": "",
"Names": [],
"Overline": false,
"Size": {
"IsEmpty": true,
"Type": 0,
"Unit": {
"IsEmpty": true,
"Type": 1,
"Value": 0
}
},
"Strikeout": false,
"Underline": false
},
"ForeColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"Height": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"IsEmpty": true,
"RegisteredCssClass": "",
"Width": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"Site": null,
"Container": null
},
"ControlStyleCreated": true,
"CssClass": "",
"Style": {
"Keys": [],
"Count": 0,
"Value": null
},
"Enabled": true,
"EnableTheming": true,
"Font": {
"Bold": false,
"Italic": false,
"Name": "",
"Names": [],
"Overline": false,
"Size": {
"IsEmpty": true,
"Type": 0,
"Unit": {
"IsEmpty": true,
"Type": 1,
"Value": 0
}
},
"Strikeout": false,
"Underline": false
},
"ForeColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"HasAttributes": false,
"Height": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"SkinID": "",
"TabIndex": 0,
"ToolTip": "",
"Width": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"ClientIDMode": 0,
"ClientID": "502",
"ID": "502",
"EnableViewState": true,
"ViewStateMode": 0,
"NamingContainer": null,
"BindingContainer": null,
"DataItemContainer": null,
"DataKeysContainer": null,
"Page": null,
"RenderingCompatibility": {
"Major": 4,
"Minor": 5,
"Build": -1,
"Revision": -1,
"MajorRevision": -1,
"MinorRevision": -1
},
"TemplateControl": null,
"Parent": null,
"TemplateSourceDirectory": "/Survey",
"AppRelativeTemplateSourceDirectory": "~/Survey/",
"Site": null,
"Visible": true,
"UniqueID": "502",
"Controls": [],
"ValidateRequestMode": 0
},
{
"BackImageUrl": "",
"Caption": "",
"CaptionAlign": 0,
"CellPadding": -1,
"CellSpacing": -1,
"GridLines": 0,
"HorizontalAlign": 0,
"SupportsDisabledAttribute": false,
"Rows": [],
"AccessKey": "",
"Attributes": {
"Keys": [],
"Count": 0,
"CssStyle": {
"Keys": [],
"Count": 0,
"Value": null
}
},
"BackColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"BorderColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"BorderWidth": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"BorderStyle": 0,
"ControlStyle": {
"BackImageUrl": "",
"CellPadding": -1,
"CellSpacing": -1,
"GridLines": 0,
"HorizontalAlign": 0,
"BackColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"BorderColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"BorderWidth": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"BorderStyle": 0,
"CssClass": "",
"Font": {
"Bold": false,
"Italic": false,
"Name": "",
"Names": [],
"Overline": false,
"Size": {
"IsEmpty": true,
"Type": 0,
"Unit": {
"IsEmpty": true,
"Type": 1,
"Value": 0
}
},
"Strikeout": false,
"Underline": false
},
"ForeColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"Height": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"IsEmpty": true,
"RegisteredCssClass": "",
"Width": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"Site": null,
"Container": null
},
"ControlStyleCreated": true,
"CssClass": "",
"Style": {
"Keys": [],
"Count": 0,
"Value": null
},
"Enabled": true,
"EnableTheming": true,
"Font": {
"Bold": false,
"Italic": false,
"Name": "",
"Names": [],
"Overline": false,
"Size": {
"IsEmpty": true,
"Type": 0,
"Unit": {
"IsEmpty": true,
"Type": 1,
"Value": 0
}
},
"Strikeout": false,
"Underline": false
},
"ForeColor": {
"R": 0,
"G": 0,
"B": 0,
"A": 0,
"IsKnownColor": false,
"IsEmpty": true,
"IsNamedColor": false,
"IsSystemColor": false,
"Name": "0"
},
"HasAttributes": false,
"Height": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"SkinID": "",
"TabIndex": 0,
"ToolTip": "",
"Width": {
"IsEmpty": true,
"Type": 1,
"Value": 0
},
"ClientIDMode": 0,
"ClientID": "503",
"ID": "503",
"EnableViewState": true,
"ViewStateMode": 0,
"NamingContainer": null,
"BindingContainer": null,
"DataItemContainer": null,
"DataKeysContainer": null,
"Page": null,
"RenderingCompatibility": {
"Major": 4,
"Minor": 5,
"Build": -1,
"Revision": -1,
"MajorRevision": -1,
"MinorRevision": -1
},
"TemplateControl": null,
"Parent": null,
"TemplateSourceDirectory": "/Survey",
"AppRelativeTemplateSourceDirectory": "~/Survey/",
"Site": null,
"Visible": true,
"UniqueID": "503",
"Controls": [],
"ValidateRequestMode": 0
}
]
GetResult(List<Table> list)
List<Table>
我觉得这让你失败了,它是来自System.Web.UI.WebControls的一个类,一个webform控件.把它改成List<SurveyDetails>
试试
我没有50分,无法添加评论,所以只需留下答案
相关文章:
- jQuery匹配JSON对象的部分文本
- 如何在Javascript中将JSon对象转换为数组
- 我可以在json对象中添加一个函数吗
- 使用JS将数组转换为json对象
- 如何为json对象中的段发送array[]
- 将JSON对象传递给angular指令
- 更改JSON对象的结构
- 访问JSON对象内部的数组元素
- 在ejs-partial中对JSON对象进行迭代
- 遍历 JSON 对象并检查 URL 是否以某个值结尾
- 访问嵌套JSON对象的键,其中键是动态的
- json对象中缺少对象循环
- 发送json对象或使用express路由呈现视图
- 在play2框架中向json对象添加下拉列表项
- 元素名称上带有短划线 (-) 字符的 Json 对象
- autocomplete不接受源的json对象
- 如何在javascript中创建动态json对象
- 在使用客户端脚本时拾取JSON对象
- 如何通过json对象数组为嵌套对象赋值
- 构造JSON对象