2012-09-26 75 views
1

我有一個REST Hello World服務啓動並運行ServiceStack。ServiceStack JSON根名稱

它目前從測試對象,看起來返回JSON這樣的:

{"Name":"Value"} 

的目標很簡單:

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

有誰我該怎麼裝飾類強制在根名稱JSON所以它看起來像這樣:

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

謝謝。

+1

'new {root = new TestResponse(){Name =「Value」}}'或'new {r oot = new {Name =「Value」}}' –

+0

太好了,謝謝。 –

回答

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" } } };