C# ServiceStack JSON根名称
我已经使用ServiceStack启动并运行了REST hello world服务 它当前从测试对象返回JSON,如下所示:C# ServiceStack JSON根名称,c#,json,
servicestack,C#,Json,
servicestack,我已经使用ServiceStack启动并运行了REST hello world服务 它当前从测试对象返回JSON,如下所示: {"Name":"Value"} 目标很简单: public class TestResponse { public string Name { get; set; } } 有人知道我如何修饰类,使其在JSON中强制使用根名称,使其看起来像这样: { root:{"Name":"Value"} } 谢谢。返回的JSON与您填充的DTO的确切形状相匹配(即首先是DT
{"Name":"Value"}
目标很简单:
public class TestResponse { public string Name { get; set; } }
有人知道我如何修饰类,使其在JSON中强制使用根名称,使其看起来像这样:
{ root:{"Name":"Value"} }
谢谢。返回的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; }
}
new{root=newtestresponse(){Name=“Value”}}
或new{root=new{Name=“Value”}}
return new TestResponse { Root = { { "Name", "Value" } } };