我正在评估ServiceStack,我已经按照一些例子。但是,返回的JSON看起来像{key:arrayofobjects},而不仅仅是{arrayofobjects}。
如何返回它以使JSON更干净且没有Response属性作为键?
这是返回的内容:
{
suburb:
{
id: 1753,
name: "Quorrobolong",
postcode: "2325"
}
}
这是我希望返回的内容:
{
id: 1753,
name: "Quorrobolong",
postcode: "2325"
}
服务:
public class SuburbService : Service
{
public ICacheClient CacheClient { get; set; }
public object Get(Suburb request)
{
string cacheKey = UrnId.Create<Suburb>(request.Id);
var resp = Request.ToOptimizedResultUsingCache<SuburbResponse>(CacheClient, cacheKey, () =>
{
return new SuburbResponse { Suburb = Db.LoadSingleById<Suburb>(request.Id) };
});
return resp;
}
}
回应:
public class SuburbResponse : IHasResponseStatus
{
public Suburb Suburb { get; set; }
public ResponseStatus ResponseStatus { get; set; }
}
ORMLite POCO:
[Route("/suburbs", "PUT,POST,PATCH")]
[Route("/suburbs/{Id}")]
public partial class Suburb : IHasId<long>
{
[Alias("SuburbID")]
[AutoIncrement]
public long Id { get; set;}
[Required]
public string Name { get; set;}
[Required]
public string Postcode { get; set;}
}
答案 0 :(得分:3)
JSON ServiceStack正在生产干净;它正在产生你要求的响应。但是为了获得您想要的更平坦的结构,您应该SuburbResponse
继承Suburb
而不是包含它。
public class SuburbResponse : Suburb, IHasResponseStatus
{
public ResponseStatus ResponseStatus { get; set; }
}
在您的回访中,您可以使用ServiceStack的自动映射器将Suburb
映射到响应类型SuburbResponse
。
ServiceStack 3: 你可以learn about v3 auto-mapper here。
using ServiceStack.Common;
...
return Db.LoadSingleById<Suburb>(request.Id).TranslateTo<SuburbResponse>();
ServiceStack 4: 你可以learn about v4 auto-mapper here。
using ServiceStack;
...
return Db.LoadSingleById<Suburb>(request.Id).ConvertTo<SuburbResponse>();
希望这会有所帮助。如果您有任何问题或进一步的疑问,请在评论中告诉我。