Nathiel Paulino
Nathiel Paulino

Reputation: 544

Js syntax to C# with aspnet mvc

I'm trying to serialize an Array to use with a Google Chart lib.

var data = google.visualization.arrayToDataTable([
      ['Task', 'Hours per Day'],
      ['Work',     11],
      ['Eat',      2],
      ['Commute',  2],
      ['Watch TV', 2],
      ['Sleep',    7]
    ]);

But when I tried to use KeyValuePair I can only use two types of data to my all collection and in the example above, the first Row has two strings. How can I render this information and convert to array to match the JS data ?

Upvotes: 0

Views: 69

Answers (1)

R. StackUser
R. StackUser

Reputation: 2065

If you have data which is not strongly typed (the value can either be a string or an integer), you can always use generic objects. 3 different c# examples:

List of Key/Value pairs:

var data = new List<KeyValuePair<string, object>>(){
     new KeyValuePair<string,object>("Task", "Hours per Day"),
     new KeyValuePair<string,object>("Work", 11) // etc...            
     };

2D Object array

var data = new object[,] {
                {"Task", "Hours per Day"},
                {"Work", 11} // etc...            
                };

Dictionary

  var data = new Dictionary<string, object>(){
                 {"Task", "Hours per Day"},
                {"Work", 11} // etc...            
            };

There are many ways to do what you want, depending on how you want to parse / convert / deserialize your data.

Upvotes: 1

Related Questions