0

我正在尝试在 asp.net 项目中设置 Web 服务以返回 JSON 格式的请求。

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 回答