我已经创建了一个wcf,但是我需要更改json响应的格式结束。
这是回复:
{"LoginResponseResult":{"responsecode":"1","responsemessage":"success","UserDetails":{"firstname":"aaa","lastname":"aaa"}}}
我想要展示的内容
{"LoginResponse":{"responsecode":"1","responsemessage":"success","UserDetails":{"firstname":"aaa","lastname":"aaa"}}}
这是我的班级
namespace JSONWCF
{
[DataContract]
public class LoginResponse
{
[DataMember(Name = "responsecode", Order = 0)]
public string Responsecode
{
get;
set;
}
[DataMember(Name = "responsemessage", Order = 1)]
public string Responsemessage
{
get;
set;
}
[DataMember(Name = "UserDetails", Order = 2)]
public UserDetails details
{
get;
set;
}
}
[DataContract]
public class UserDetails
{
[DataMember(Name = "firstname", Order = 0)]
public string Firstname
{
get;
set;
}
[DataMember(Name = "lastname", Order = 1)]
public string Lastname
{
get;
set;
}
}
}
为什么" loginresponse"添加"结果"? 我可以删除它吗?
服务联系
[OperationContract]
[WebInvoke(
Method = "POST",
UriTemplate = "LoginMobile",
BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
LoginResponse LoginResponse(Login LoginRequest);
public LoginResponse LoginResponse(Login LoginRequest)
{
LoginResponse LoginResponse = new LoginResponse();
UserDetails details = new UserDetails();
details.Firstname = "aaa";
details.Lastname = "aa";
LoginResponse.details = details;
LoginResponse.Responsecode = "1";
LoginResponse.Responsemessage = "success";
return LoginResponse;}
答案 0 :(得分:2)
您可以按如下方式更改ServiceContract
[OperationContract]
[WebInvoke(
Method = "POST",
UriTemplate = "LoginMobile",
BodyStyle = WebMessageBodyStyle.Bare,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
LoginResponse LoginResponse(Login LoginRequest);
它会返回这样的东西
{"responsecode":"1","responsemessage":"success","UserDetails":{"firstname":"aaa","lastname":"aaa"}}
我认为您无论如何都不需要使用LoginResponseResult
或LoginResponse
。
您在客户端面临的问题是什么?只需将您的服务与ChannelFactory一起使用,就不应该遇到任何问题。
我认为因为如果你使用WebMessageBodyStyle.Wrapped
,WCF Rest引擎会在包装时自动将Result
附加到返回的JSON中,我们可能无法控制它。