asp.net 2.0 asmx WebService Exclude __type in JSON Response
asp.net, asp.net-ajax, json, web-services
Solution
The way to do this is follows.
public class MyClassName
{
private string _item1 = string.Empty;
private string _item2 = string.Empty;
public string item1 = { get { return _item1; } set { _item1 = value; } }
public string item2 = { get { return _item2; } set { _item2 = value; } }
protected internal MyClassName() { } //add a protected internal constructor to remove the returned __type attribute in the JSON response
}
public class MyClassName_List : List<MyClassName>
{
public MyClassName_List() {}
}
I hope this helps someone else too!
Problem
I am trying to determine how to exclude `__type` in my JSON response from an asmx WebService. The class that I am returning is constructed as follows. ``` public class MyClassName { private string _item1 = string.Empty; private string _item2 = string.Empty; public string item1 = { get { return _item1; } set { _item1 = value; } } public string item2 = { get { return _item2; } set { _item2 = value; } } } public class MyClassName_List : List<MyClassName> { public MyClassName_List() {} } ``` I then have a Data Access Layer and Business Logic Layer that returns a populated instance of `MyClassName_List`. My WebMethod is setup as follows. ``` using System; using System.Collections.Generic; using System.Web; using System.Web.Services; [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] [System.Web.Script.Services.ScriptService] public class MyClassName_WebService : System.Web.Services.WebService { [WebMethod] public MyClassName_List GetList() { return MyClassName_List_BusinessLogicLayer.GetList(); } } ``` The JSON object return is structured as follows. ``` [ { item1: "item1-1 text", item2: "item1-2 text", __type: "NamespaceUsed.MyClassName" }, { item1: "item2-1 text", item2: "item2-2 text", __type: "NamespaceUsed.MyClassName" }, ] ``` I just want to return the JSON object as follows. ``` [ { item1: "item1-1 text", item2: "item1-2 text" }, { item1: "item2-1 text", item2: "item2-2 text" } ] ``` I have tried the suggestions from here, but I can not seem to implement it properly. Any help on this is much appreciated!