简体   繁体   中英

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM