0

i have create a wcf all is working but i need to change the format of the json response is 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"? 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;} 
4
  • Maybe [DataContract(Name = "LoginResponse")]? Commented Sep 16, 2014 at 6:42
  • not working still the same output Commented Sep 16, 2014 at 6:44
  • I have a solution. Please share your ServiceContract class Commented Sep 16, 2014 at 7:05
  • i updated the post please check Commented Sep 16, 2014 at 7:15

1 Answer 1

2

You may change the ServiceContract as follows

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

I assume you do not need to use LoginResponseResult or LoginResponse anyway.

What is the problem that you are facing at the client side? Simply use your service with ChannelFactory, you should not face any issues.

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.

Sign up to request clarification or add additional context in comments.

2 Comments

is there anyway i could LoginResponse?
I don't think you will have a control over that without doing a lot of overriding etc. Believe me, you don't want to go there just to remove Result string from the JSON.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.