简体   繁体   English

如何在wcf中更改格式json响应

[英]how to change the format json response in wcf

i have create a wcf all is working but i need to change the format of the json response is end. 我已经创建了一个wcf,一切正常,但是我需要更改json响应的格式为end。

This is the response: 这是响应:

{"LoginResponseResult":{"responsecode":"1","responsemessage":"success","UserDetails":{"firstname":"aaa","lastname":"aaa"}}}

what i want to display 我想展示什么

{"LoginResponse":{"responsecode":"1","responsemessage":"success","UserDetails":{"firstname":"aaa","lastname":"aaa"}}}

here is my class 这是我的课

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

why is that "loginresponse" is added with "result"? 为什么在“ loginresponse”后面加上“ result”? can i remove it and how? 我可以删除它吗?

service contact 服务联系

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

You may change the ServiceContract as follows 您可以按以下方式更改ServiceContract

[OperationContract] [WebInvoke( Method = "POST", UriTemplate = "LoginMobile", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] LoginResponse LoginResponse(Login LoginRequest);

It will return something like this {"responsecode":"1","responsemessage":"success","UserDetails":{"firstname":"aaa","lastname":"aaa"}} 它将返回如下内容: {"responsecode":"1","responsemessage":"success","UserDetails":{"firstname":"aaa","lastname":"aaa"}}

I assume you do not need to use LoginResponseResult or LoginResponse anyway. 我认为您仍然不需要使用LoginResponseResultLoginResponse

What is the problem that you are facing at the client side? 您在客户端面临的问题是什么? Simply use your service with ChannelFactory, you should not face any issues. 只需将服务与ChannelFactory一起使用,就不会遇到任何问题。

I think because if you use WebMessageBodyStyle.Wrapped , WCF Rest engine automatically appends a Result to the returning JSON when it is being wrapped and we might not have control over it. 我认为是因为,如果您使用WebMessageBodyStyle.Wrapped ,则WCF Rest引擎在进行包装时会自动将Result附加到返回的JSON上,而我们可能无法控制它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM