ServiceStack嵌套数组错误:KeyValueDataContractDeserializer:转换为类型时出错:类型定义应以“{”开头

时间:2013-04-24 21:37:55

标签: c# rest angularjs servicestack

尝试将嵌套数组发布到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);

1 个答案:

答案 0 :(得分:1)

您在QueryString和Request Body中双重发布请求。在QueryString中,使用JSV Format解析复杂类型。

将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
        }
    ]
}
*/