0

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

1 Answer 1

1

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;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.