0

JSON 形式で要求を返すように、asp.net プロジェクトで Web サービスをセットアップしようとしています。

Web サービス メソッドのコードは次のとおりです。

    public string NavigationItems()
    {
        using (TTConnection connection = new TTConnection("ClientDb"))
        {
            if (MySession.Current.MyClub == null)
                return USER_ERROR;
            MiddleTier.WebNavigation.WebNavigationGenerator gen = MySession.Current.NavGenerator;
            List<NavigationElement> list = gen.GetNavigation();
            string json = JsonConvert.SerializeObject(list);
            return json;
        }
    }

NavigationElement オブジェクトには、次のコンストラクターとプロパティがあります

  public NavigationElement(string pUrl, int pSequence, string pLinkText, string pDescription,int pUid)
    {
        url = pUrl;
        sequence = pSequence;
        linkText = pLinkText;
        description = pDescription;
        uid = pUid;
    }

リクエストは次の JSON を返します

Content-Type:application/json; charset=utf-8;
{"d":"[{\"elements\":[],\"url\":\"~/Welcome.aspx\",\"sequence\":1,\"linkText\":\"Home\",\"description\":\"Home Page\",\"uid\":1,\"parent\":null},{\"elements\":[],\"url\":\"~/About.aspx\",\"sequence\":2,\"linkText\":\"About\",\"description\":\"About Us\",\"uid\":2,\"parent\":null},{\"elements\":[{\"elements\":[{\"elements\":[],\"url\":\"~/Member/ClubHomeSub/ClubTheme.aspx\",\"sequence\":1,\"linkText\":\"Club Theme\",\"description\":\"Club Theme\",\"uid\":5,\"parent\":null},{\"elements\":[],\"url\":\"~/Member/ClubHomeSub/ClubPages.aspx\",\"sequence\":2,\"linkText\":\"Club Pages\",\"description\":\"Club Pages\",\"uid\":8,\"parent\":null}],\"url\":\"~/Member/ClubHomeSub/ClubAdmin.aspx\",\"sequence\":1,\"linkText\":\"Club Admin\",\"description\":\"Club Admin\",\"uid\":4,\"parent\":null}],\"url\":\"~/Member/ClubHome.aspx\",\"sequence\":3,\"linkText\":\"Club Home\",\"description\":\"Club Home\",\"uid\":3,\"parent\":null},{\"elements\":[],\"url\":\"~/Member/GameAnalysis.aspx\",\"sequence\":4,\"linkText\":\"Game Analysis\",\"description\":\"Game Analysis\",\"uid\":6,\"parent\":null},{\"elements\":[],\"url\":\"~/Member/SkillAquisition.aspx\",\"sequence\":5,\"linkText\":\"Skill Aquisition\",\"description\":\"Learn new game skills\",\"uid\":7,\"parent\":null}]"}

これは有効ですが、非常に長い文字列を持つオブジェクトとして解釈されます

 d: "[{"elements":[],"url":"~/Welcome.aspx","sequence":1,"linkText":"Home","description":"Home Page","uid":1,"parent":null},{"elements":[],"url":"~/About.aspx","sequence":2,"linkText":"About","description":"About Us","uid":2,"parent":null},{"elements":[{"elements":[{"elements":[],"url":"~/Member/ClubHomeSub/ClubTheme.aspx","sequence":1,"linkText":"Club Theme","description":"Club Theme","uid":5,"parent":null},{"elements":[],"url":"~/Member/ClubHomeSub/ClubPages.aspx","sequence":2,"linkText":"Club Pages","description":"Club Pages","uid":8,"parent":null}],"url":"~/Member/ClubHomeSub/ClubAdmin.aspx","sequence":1,"linkText":"Club Admin","description":"Club Admin","uid":4,"parent":null}],"url":"~/Member/ClubHome.aspx","sequence":3,"linkText":"Club Home","description":"Club Home","uid":3,"parent":null},{"elements":[],"url":"~/Member/GameAnalysis.aspx","sequence":4,"linkText":"Game Analysis","description":"Game Analysis","uid":6,"parent":null},{"elements":[],"url":"~/Member/SkillAquisition.aspx","sequence":5,"linkText":"Skill Aquisition","description":"Learn new game skills","uid":7,"parent":null}]"
4

1 に答える 1

1

シリアライザーをまったく使用する必要がないことがわかりました。型オブジェクトを返すだけです。

public object NavigationItems()
{
    using (TTConnection connection = new TTConnection("ClientDb"))
    {
        if (MySession.Current.MyClub == null)
            return USER_ERROR;
        MiddleTier.WebNavigation.WebNavigationGenerator gen = MySession.Current.NavGenerator;
        List<NavigationElement> list = gen.GetNavigation();
        return list;
    }
}
于 2013-09-06T02:08:34.957 に答える