2

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

我得到的错误是:

KeyValueDataContractDeserializer:转换为类型时出错:类型定义应以“{”开头,期望序列化类型“DeployServer”,字符串以: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);
4

1 回答 1

1

您在 QueryString 和请求正文中双重发布请求。在 QueryString 中使用JSV 格式解析复杂类型时。

将 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
        }
    ]
}
*/
于 2013-04-24T23:11:25.037 回答