javascript - Convert c# datatable to json array to pass to Google charts -


i want pass c# datatable result chart error saying 'argument given addrows must either number or array'

c#

     string result = "";         result = "[";         foreach (datarow row in dt.rows)         {             result += "['"+row[0]+"',"+row[1]+"],";         }         result = result.trimend(',')+"]";          txtjsondata.value = result; 

javacsript

         var data = new google.visualization.datatable();           data.addcolumn('string', 'page');           data.addcolumn('number', 'followers');           var chartdata = document.getelementbyid('txtjsondata').value;           data.addrows(chartdata); 

sampledata

"[['@flu',401],['@weightloss',1068],['@heartdiseases',223],['@diabetesfacts',356]]" 

your variable chartdata contains string. need parse before passing addrows.

data.addrows(json.parse(chartdata)); 

json.parse() takes string, , return corresponding javascript object. if code needs run on internet explorer <= 8 (which doesn't include json related functions), need include json2.js, can https://github.com/douglascrockford/json-js


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -