Ashok
Ashok

Reputation: 611

how to change JSON response format

Here is a WCF method to send response in JSON format.

[OperationContract(Name = "Employee")]
[WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.WrappedResponse, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = "person/{name}")]
Person GetPersonData(string name);

Here is the format I'm getting:

{"EmployeeResult":{"Age":31,"Name":"testuser"}}

I need something like below:

{"Employee":{"Age":31,"Name":"testuser"}}

I tried to change

BodyStyle = WebMessageBodyStyle.Bare

but getting below Format:

{"Age":31,"Name":"testuser"}

Is there anyother thing I need to change ? I'm working on .net framework 4.5

Thanks

Upvotes: 1

Views: 753

Answers (1)

Maku
Maku

Reputation: 1558

You can use MessageParameterAttribute:

[OperationContract(Name = "Employee")]
[WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.WrappedResponse, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = "person/{name}")]
[return:MessageParameter(Name = "Employee")]
Person GetPersonData(string name);

Upvotes: 3

Related Questions