Link to home
Start Free TrialLog in
Avatar of gladstonesheeba
gladstonesheeba

asked on

Pass an array to jquery function to build chart

Hi,
  I have a  Dictionary<string, int>  variable  users , which gets the data a total order count  in this format

user1,0
user2,3
user3,6
...
..

Am building a chart from this data , so i need to transfer the data  to a jquery function  in this 2 kinds of  format

First format
['user1',0],['user2',3],['user3',6]

second format - for x axis
'user1','user2','user3'

Could you please help me how can i get those  formats ?.
ASKER CERTIFIED SOLUTION
Avatar of Najam Uddin
Najam Uddin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
JQuery interacts with the DOM, not with your server side code.  So, you'll need to use something like AJAX to get your Dictionary.  In order to do that, you need to convert your Dictionary to JSON .

Once you have your JSON string in Jquery, you can use $.parseJSON() to convert it into an array.  That's the first format you're requesting.  The second format is a string by using .toString() on the array.

Here's a general idea of the Ajax code:

$.ajax({
                        type: "GET",
                        url: "yourcodefile.aspx/methodName",
                        dataType: "json",
                        success: function(response) {
                          //verify format you're receiving
                            console.log(response)
                            var responseArray = $.parseJSON(response);
                            var responseString = responseArray.toString();
                        }
                    });

Open in new window


You method would look something like:

 [WebMethod]
public static string methodName()
{

  //...

    return JsonConvert.SerializeObject( myDictionary );
}

Open in new window