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 ?.
gladstonesheebaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Najam UddinCommented:
var a=  string.Join(",", dict.Select(d => string.Format("['{0}',{1}]", d.Key, d.Value)));

similarly you can create second one

var a=  string.Join(",", dict.Select(d => string.Format("'{0}{1}'", d.Key, d.Value)));
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zephyr_hex (Megan)DeveloperCommented:
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

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.