Json 反序列化 BodyStyle Wrap issue using IPWorks nSoftware



我正在使用IPWorks nsoftware来创建服务。在其中,调用我正在使用的服务

        Rest rest = new Rest();
        rest.Accept = "application/json";
        rest.ContentType = "application/json";
        rest.User = "UserName";
        rest.Password = "Password";
        rest.Get(@"http://Foo.com/roles.json");
        string result = rest.TransferredData;
        var listRoles = JsonSerializer.DeserializeFromString<List<role>>(result);

我以字符串形式收到 Json 响应

[{"

角色":{"名称":"管理员","created_at":"2012-02-11T09:53:54-02:00","updated_at":"2012-04-29T23:43:47-04:00","id":1"}},{"角色":{"名称":"NormalUser","created_at":"2013-02-11T08:53:54-02:00","updated_at":"2013-04-29T23:43:47-03:00","id":2"}}]

在这里,json 字符串包含我的域对象"角色",它被附加到我的响应中(即消息的正文样式被包装)。我正在使用ServiceStack.Text的反序列化程序将响应字符串转换为我的对象。但是由于它是包裹的,所以去浆是不正确的。

我在这里缺少什么吗?是否有任何"BodyStyle"属性可以添加到 Rest 请求中?

GitHubRestTests 展示了使用 ServiceStack 的 JSON 序列化程序反序列化第三方 json API 的一些不同方法。

如果要将其反序列化为类型化的 POCO,那么根据 JSON 有效负载判断,类型化的 POCO 应如下所示:

public class RolePermissionWrapper 
{
    public Role Role { get; set; }
    public Permission Permission { get; set; }
}
public class Role 
{ 
    public long Id { get; set; } 
    public string Name { get; set; } 
    public DateTime? Created_At { get; set; } 
    public DateTime? Updated_At { get; set; } 
} 
var listRoles = JsonSerializer.DeserializeFromString<List<RolePermissionWrapper>>(result);

最新更新