意外结果,在 ASP.Net 中解析 JSON 对象的数组

Unexpected results, Parse Array of JSON Objects in ASP.Net

本文关键字:JSON 对象 数组 结果 ASP Net 意外      更新时间:2023-09-26

我正在尝试学习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分,无法添加评论,所以只需留下答案