2

I want return JSON from Web Service 4.0. I am creating an application for IPhone. IPhone developer want a web service which response in JSON.

I have done lot of R&D and found some solution but it’s not right.It returns JSON with XML header. I want pure JSON Web Service.

Please help me creating JSON web service. If you provide me some code sample then it’s helpful for me.

Thanks in advance.

Regards Jelly

jellysaini
  • 158
  • 5
  • 15

3 Answers3

1

Very similar question here with good links: How do I get MVC WebAPI to return JSON instead of XML using Chrome.

If you have the option of using the new ASP.NET MVC 4.0 Web API to build the service, then it will serialize xml, or json, for you with very little effort.

The service consumer can specify the http accept header (accept: application/json) and that will choose the right formatter. Optionally, if you only ever want to return JSON, you can do that, too.

The samples, from the same ASP.NET MVC 4.0 link above, are a really good jump start.

Community
  • 1
  • 1
Zach Bonham
  • 6,759
  • 36
  • 31
1

One way is to use WCF to create RESTful services with JSON serialization. Examples are many, but you can start with a simple one. You just need to be careful to specify RequestFormat = WebMessageFormat.Json and ResponseFormat = WebMessageFormat.Json on your web methods (GET, POST, etc).

Another important property you need make sure is set is the BodyStyle, on the WebInvokeAttribute (on your web methods). The value should be WebMessageBodyStyle.Bare, so the framework will not decorate it with extra XML elements.

Other options include WCF Data Services (OData protocol). Version 5.0 has just been released (http://msdn.microsoft.com/en-us/data/odata.aspx).

Marcel N.
  • 13,726
  • 5
  • 47
  • 72
0

I would advise you to use OData RESTful services as suggested by marceln. Just go through some samples online and you'll be amazed to see how easy they are to build and how useful they actually are! Here is a sample project on CodeProject for your quick reference: http://www.codeproject.com/Articles/393623/OData-Services

DotNetFreak
  • 166
  • 5