1

我已经使用 ServiceStack 启动并运行了一个 REST hello world 服务。

它当前从如下所示的测试对象返回 JSON:

{"Name":"Value"}

对象很简单:

public class TestResponse { public string Name { get; set; } }

有谁可以装饰类以强制在 JSON 中使用根名称,使其看起来像这样:

 { root:{"Name":"Value"} }

谢谢。

4

1 回答 1

2

返回的 JSON 与您填充的 ​​DTO 的确切形状相匹配(即首先是 DTO 的角色)。因此,您应该更改 DTO 以表示您想要的确切形状,例如

public class TestResponse {
    public TestRoot Root { get; set; }
}
public class TestRoot {
    public string Name { get; set; }
}

然后您可以按预期返回它:

return new TestResponse { Root = new TestRoot { Name = "Value" } };   

或者,如果您愿意,也可以使用字典:

public class TestResponse {
    public TestResponse() {
       this.Root = new Dictionary<string,string>();
    }
    public Dictionary<string,string> Root { get; set; }
}

并返回:

return new TestResponse { Root = { { "Name", "Value" } } };   
于 2012-09-26T20:26:00.393 回答