我正在尝试在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对象具有以下构造函数&amp;特性
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}]"
答案 0 :(得分: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;
}
}