0

I have a web-service called SalesService, which returns the info as a "SalesInfo" instance. This web-service will be called from a Windows application.

I want to know whether its possible to send the result from web-service in a JSON format?.

Remember here its being called from a Windows app not from a web app. I want to know how we can send JSON from webservice to a windows app.. so that XML serialization wont happen.

Thanks

asked Nov 5, 2012 at 18:06
3
  • 1
    Yes, you can return JSON. How you do it depends on the type of web service you have, WCF, WebAPI, etc. Commented Nov 5, 2012 at 18:11
  • Have you checked out JSON.NET? You should be able to create your JSON object, return it as a string, and then parse it in the browser using JSON.parse() Commented Nov 5, 2012 at 18:12
  • Is this an ASMX service? Commented Nov 5, 2012 at 19:04

2 Answers 2

2

It depends on the type of web service you have.

  • If it's WCF, you can use the WebInvoke attribute and WebMessageFormat.Json to set a JSON response. See this post for an example.

  • If you're using an ASP.NET MVC project and want to return JSON, use the Json object:

For example:

public JsonResult Index()
{
 return Json(new { name = "John Doe" });
}
  • If you're using WebAPI, you need to set the Content-Type header on the request, and WebAPI will take care of the rest.
answered Nov 5, 2012 at 18:19

Comments

0

Sending a response in JSON is possible and is relatively straight forward. It is up to the client to decipher it.

The windows app will need to deserialise the JSON result from the webapp. There is a JSON Library in .NET 4 in the System.Runtime.Serialization.Json namespace. If you need it for an earlier verion, you may need to check out JSON.Net - http://james.newtonking.com/pages/json-net.aspx

answered Nov 5, 2012 at 18:13

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.