简体   繁体   中英

JSON serializer with nested object lists

I'm trying to setup a web service in a asp.net project to return requests in a JSON format.

the web service method code is as follows:

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

The NavigationElement object has the following constructor & properties

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

and the request returns the following 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}]"}

Which is valid but gets interpreted as an object with a very long string

 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}]"

Turns out you don't need to use a serializer at all. You just return type object.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM