I’m creating a web service using ASP.NET, C#, currently it is giving XML, but I’m going to get JSON, this is how I create my webmtheod:
[Webservice(Namespace="http://myurl)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfiles_1)]
[System.ComponentModel.ToolboxItem(false)]
[WebMehod]
public string myfunction()
{
string r = "......";
return r;
}
these are in an ASMX file, which I call it in my browser
If you want to return JSON from your method you will need to use the ScriptMethod attribute.
Structure your method like this, notice the
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]attribute.At the moment, this method is returning a
string, which can be aJSONstructured string. However, you may be better to return an object, which can be parsed asJSON.List<string>as well as Class’s with standard data-types, likeintegers,stringsetc are great for this. You can then return just that object. theScriptMethodtakes care of transforming it intoJSON.For example:
The Class you want to return:
And your method to return a populated
MyJsonThe return
JSONwill be structured just like the class. The property names will be used too, and yourList<string>will become an arrayCall this using AJAX. JQuery in this case: