gpt4 book ai didi

c# - ServiceStack 嵌套数组错误 : KeyValueDataContractDeserializer: Error converting to type: Type definitions should start with a '{'

转载 作者:太空宇宙 更新时间:2023-11-03 13:43:25 25 4
gpt4 key购买 nike

尝试将嵌套数组发布到 ServiceStack 剩余端点时出现错误。

我得到的错误是:

KeyValueDataContractDeserializer: Error converting to type: Type definitions should start with a '{', expecting serialized type 'DeployServer', got string starting with: object Object]"

我的类结构如下:

public class DeployEnvironment
{
public string Id { get; set; }
public string ProjectId { get; set; }
public string EnvironmentName { get; set; }
public List<DeployServer> ServerList { get; set; }

public DeployEnvironment()
{
this.ServerList = new List<DeployServer>();
}
}
public class DeployServer
{
public string Id { get; set; }
public string EnvironmentId { get; set; }
public string ServerName { get; set; }
}

邮政编码很简单:

public object Post(DeployEnvironment environment)
{
if (string.IsNullOrEmpty(environment.Id))
{
return _projectManager.CreateEnvironment(environment.ProjectId, environment.EnvironmentName, environment.ServerList);
}
else
{
return _projectManager.UpdateEnvironment(environment.Id, environment.ProjectId, environment.EnvironmentName, environment.ServerList);
}
}

这是我发送的 JSON,我觉得没问题:

{
"id": "300b1bd2-af16-47bb-a167-407ec8966167",
"projectId": "03b5635a-7eb8-4aeb-80e0-461f29c4488c",
"environmentName": "QA",
"serverList": [
{
"id": "6024e867-f858-47cb-93f4-dd592adb02af",
"environmentId": "300b1bd2-af16-47bb-a167-407ec8966167",
"serverName": "Server1"
},
{
"id": "efcc3a14-3d4a-4990-b106-d5a81188ee04",
"environmentId": "300b1bd2-af16-47bb-a167-407ec8966167",
"serverName": "Server2"
}
]
}

事实上,这与我从 get 中提取的 JSON 相同,我只是想将其发回

我做错了什么吗?

谢谢

更新为原始 HTTP 请求:

POST http://local-host:55052/api/project/03b5635a-7eb8-4aeb-80e0-461f29c4488c/environment?environmentName=QA&id=300b1bd2-af16-47bb-a167-407ec8966167&serverList=%5Bobject+Object%5D,%5Bobject+Object%5D HTTP/1.1
Host: local-host:55052
Connection: keep-alive
Content-Length: 382
Accept: application/json, text/plain, */*
Origin: http://local-host:55052
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31
Content-Type: application/json;charset=UTF-8
Referer: http://local-host:55052/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: ltkmodal-Modal%20Signup=Thu%2C%2024%20Jan%202013%2021%3A47%3A32%20GMT; __test=1; ss-pid=uUvKs0YDGCPIHP1yhaUg; ss-id=7Oz3jNCeXDVoLaGT4+BM; __ngDebug=true; JSESSIONID=09245EF568D4507A3635664A6E8671DA; RememberMe=1180157474^2#-6059067053273955846

{"id":"300b1bd2-af16-47bb-a167-407ec8966167","projectId":"03b5635a-7eb8-4aeb-80e0-461f29c4488c","environmentName":"QA","serverList":[{"id":"6024e867-f858-47cb-93f4-dd592adb02af","environmentId":"300b1bd2-af16-47bb-a167-407ec8966167","serverName":"Server1"},{"id":"efcc3a14-3d4a-4990-b106-d5a81188ee04","environmentId":"300b1bd2-af16-47bb-a167-407ec8966167","serverName":"Server2"}]}

回答感谢 mythz 的回答,他是对的,我在重复发布。问题是我调用 AngularJS 资源库以这种方式保存:

$scope.environment.$save($scope.environment);

我应该这样调用它:

var saveParams = {
id: $routeParams.environmentId,
projectId: $routeParams.projectId
};
$scope.environment.$save(saveParams);

最佳答案

您在查询字符串和请求正文中重复发布了请求。当使用 JSV Format 解析 QueryString 复杂类型时.

将 URL 更改为仅选择的路由,即:

/api/project/03b5635a-7eb8-4aeb-80e0-461f29c4488c/environment

然后解析请求体应该没问题,例如:

var json = @"{""id"":""300b1bd2-af16-47bb-a167-407ec8966167"",""projectId"":""03b5635a-7eb8-4aeb-80e0-461f29c4488c"",""environmentName"":""QA"",""serverList"":[{""id"":""6024e867-f858-47cb-93f4-dd592adb02af"",""environmentId"":""300b1bd2-af16-47bb-a167-407ec8966167"",""serverName"":""Server1""},{""id"":""efcc3a14-3d4a-4990-b106-d5a81188ee04"",""environmentId"":""300b1bd2-af16-47bb-a167-407ec8966167"",""serverName"":""Server2""}]}";
var dto = json.FromJson<DeployEnvironment>();

dto.PrintDump();

输出:

/*
{
Id: 300b1bd2-af16-47bb-a167-407ec8966167,
ProjectId: 03b5635a-7eb8-4aeb-80e0-461f29c4488c,
EnvironmentName: QA,
ServerList:
[
{
Id: 6024e867-f858-47cb-93f4-dd592adb02af,
EnvironmentId: 300b1bd2-af16-47bb-a167-407ec8966167,
ServerName: Server1
},
{
Id: efcc3a14-3d4a-4990-b106-d5a81188ee04,
EnvironmentId: 300b1bd2-af16-47bb-a167-407ec8966167,
ServerName: Server2
}
]
}
*/

关于c# - ServiceStack 嵌套数组错误 : KeyValueDataContractDeserializer: Error converting to type: Type definitions should start with a '{' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16202663/

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