Convert string to Array passed as Argument to AjaxManager

74 Views Asked by At

I am trying to post an array from jQuery on an .aspx page to a method in the codebehind using Telerik AjaxManager. The problem I am having is converting the string to a list or even an array using C#. The string (after using Json.stringify in jQuery) looks like this:

string zoneString = "[[1,[["Ticket Core.AFE"],["Ticket Core.CompanyName"]]],[2,[["Ticket Core.CompanyCity"],["Ticket Core.CompanyState"]]]]"

I tried to Deserialize like this:

List<ZoneInfo> zones = JsonConvert.DeserializeObject<List<ZoneInfo>>(zonesString);

But that produces the following error:

Message=Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'COGS.Web.Modules.Admin.ReportWizard+ZoneInfo' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '[0]', line 1, position 2.

Here is the client-side code:

 var zones = [];

        for (var i = 1; i <= zoneCount; i++) 
        {
            var o = $.data(listBox, i.toString());
            if(o != undefined)
            {  
                var items = [];
                var a = o._array;
                //pull the key/value
                for (x = 0; x < a.length; x++)
                {
                    var item = a[x];
                    var value = item.get_value();

                    items.push([value]);


                }
            zones.push([i, items]);

            }
        }
        jsonZones = JSON.stringify(zones);
        var ajaxManager = $find("<%= ((RadAjaxManager)this.Page.Master.FindControl("RadAjaxManager1")).ClientID %>");
        ajaxManager.ajaxRequest('CREATEREPORT|' + jsonZones + '|' + tid);

Here is the server-side code:

protected void AjaxMgr_AjaxRequest(object sender, AjaxRequestEventArgs e)
    {
        LoggerBA.Log(DB_Context, Path.GetFileName(Request.ServerVariables["SCRIPT_NAME"]), MethodBase.GetCurrentMethod().Name, UtilityBA.LoggerLevel.Info, "IsPostBack: " + IsPostBack.ToString());

        string[] args = e.Argument.Split('|');
        string sCommand = args[0];

        switch (sCommand.ToUpper())
        {
            case "CREATEREPORT":
                string zonesString = args[1];
                List<ZoneInfo> zones = JsonConvert.DeserializeObject<List<ZoneInfo>>(zonesString);
                var template = args[2];
                string[] zone = new string[zones.Count];

                break;
        }
        InitializeWizard();
    }

Any assistance is greatly appreciated

1

There are 1 best solutions below

0
On

May not be the answer but JSON.stringify accepts a second parameter where you can specify the keys in the key value pairs: JSON.stringify(data, ['key1', 'key2', 'key3']). Alternatively that second parameter could be a function but I think that is more useful for direct character replacement.