3

__typeJSON 応答で asmx WebService から除外する方法を決定しようとしています。

私が返すクラスは次のように構築されています。

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() {}
}

次に、入力された のインスタンスを返すデータ アクセス レイヤーとビジネス ロジック レイヤーを用意しますMyClassName_List。私のWebMethodは次のように設定されています。

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();
    }
}

JSON オブジェクトの戻り値は、次のように構造化されています。

[
    {
    item1: "item1-1 text",
    item2: "item1-2 text",
    __type: "NamespaceUsed.MyClassName"
    },
    {
    item1: "item2-1 text",
    item2: "item2-2 text",
    __type: "NamespaceUsed.MyClassName"
    },
]

次のようにJSONオブジェクトを返したいだけです。

[
    {
    item1: "item1-1 text",
    item2: "item1-2 text"
    },
    {
    item1: "item2-1 text",
    item2: "item2-2 text"
    }
]

hereからの提案を試しましたが、適切に実装できないようです。これに関するヘルプは大歓迎です!

4

1 に答える 1

8

これを行う方法は次のとおりです。

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() {}
}

これが他の誰かにも役立つことを願っています!

于 2012-06-01T14:30:06.890 に答える