gpt4 book ai didi

c# - 无法反序列化数组

转载 作者:太空狗 更新时间:2023-10-29 20:31:33 27 4
gpt4 key购买 nike

从客户端发送的数据不会被反序列化。

客户:

$.ajax({
type: 'POST',
dataType: "json",
url: 'savecommentsservice',
data: { "Pid": 0, "Comments": [{ "User": "bbbbbb", "Text": "aaaaaaaa" }, { "User": "ddddddd", "Text": "ccccccccc"}] },

DTO:

public class Comment
{
public string User { get; set; }
public string Text { get; set; }
}
public class SaveCommentsRequest
{
public int Pid { get; set; }
public string SiteId { get; set; }
public string Column { get; set; }
public List<Comment> Comments { get; set; }
}

服务器:

Routes.Add<SaveCommentsRequest>("/savecommentsservice");

...

public object Execute(SaveCommentsRequest request)
{
// Why is request.Comments null !!! All other params of the request works fine

// THe string I'm putting in is generated below - just to make sure its correct
request.Comments = new List<Comment>();
request.Comments.Add(new Comment { Text = "aaaaaaaa", User = "bbbbbb" });
request.Comments.Add(new Comment { Text = "ccccccccc", User = "ddddddd" });

var thisIsWhatIPutInto$AjaxCall = JsonSerializer.SerializeToString<SaveCommentsRequest>(request);

知道有什么问题吗?为什么未填充 Comments 数组。正如您从评论中看到的那样,我使用 servicestack json 序列化程序生成了字符串。

也许我在这里遗漏了一些明显的东西?

编辑在查看 fiddler whats beeing transmitted 时,我看到了这个:

    POST http://localhost:4920/savecommentsservice HTTP/1.1
Host: localhost:4920
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:10.0.2) Gecko/20100101 Firefox/10.0.2
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: zh,en-us;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://localhost:4920/default.cshtml?viewId=3
Content-Length: 227
Cookie: ASP.NET_SessionId=slbw453revfgmwuuxyrim1an
Pragma: no-cache
Cache-Control: no-cache

SiteId=Site1&Pid=0&Column=C6&Comments%5B0%5D%5BUser%5D=bbbbbb&Comments%5B0%5D%5BText%5D=aaaaaaaa&Comments%5B1%5D%5BUser%5D=ddddddd&Comments%5B1%5D%5BText%5D=ccccccccc&Comments%5B2%5D%5BUser%5D=ggggggg&Comments%5B2%5D%5BText%5D=

在 Firebug 中:

    Parametersapplication/x-www-form-urlencoded
Column C6
Comments[0][Text] aaaaaaaa
Comments[0][User] bbbbbb
Comments[1][Text] ccccccccc
Comments[1][User] ddddddd
Comments[2][Text]
Comments[2][User] ggggggg
Pid 0
SiteId SiteId0
Source
SiteId=SiteId0&Pid=0&Column=C6&Comments%5B0%5D%5BUser%5D=bbbbbb&Comments%5B0%5D%5BText%5D=aaaaaaaa&Comments%5B1%5D%5BUser%5D=ddddddd&Comments%5B1%5D%5BText%5D=ccccccccc&Comments%5B2%5D%5BUser%5D=ggggggg&Comments%5B2%5D%5BText%5D=

问候

拉尔西

最佳答案

如您所见,jQuery 的 Ajax 默认将其提交为“application/x-www-form-urlencoded”,这是默认的 HTML 表单发布内容类型。不幸的是,它不能很好地处理嵌套对象,ServiceStack 会在 JSV Format 中期望嵌套对象。 .

因此,对于复杂类型/嵌套对象,您想要告诉 jQuery 将请求作为 JSON 发送,您可以通过指定 contentType:application/json 参数来实现,例如:

var data = { "Pid": 0, "Comments": [{ "User": "bbbbbb", "Text": "aaaaaaaa" }]};
$.ajax({
type: 'POST',
dataType: "json",
contentType: "application/json",
url: 'savecommentsservice',
data: JSON.stringify(data)
);

关于c# - 无法反序列化数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9602548/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com