Wednesday, 15 April 2015

c# - String array not serialized to json properly? -



c# - String array not serialized to json properly? -

trying pass json view.

in controller:

var array = new string[] {"123", "aaa"}; string jsonarray = jsonconvert.serializeobject(array); viewbag.jsondatalabels = jsonarray;

in view's script:

var jlabels = '@viewbag.jsondatalabels';

but json seems invalid. eg if in script:

var jlabels = '@viewbag.jsondatalabels'; alert(jlabels); // output ko : ["123","aaa"] var jlabels2 = ["aaa", "123"]; alert(jlabels2); // outputs ok : aaa, 123

why json invalid?

analyze (sample newtonsoft website)

product product = new product(); product.name = "apple"; product.expiry = new datetime(2008, 12, 28); product.sizes = new string[] { "small" }; string json = jsonconvert.serializeobject(product); //{ // "name": "apple", // "expiry": "2008-12-28t00:00:00", // "sizes": [ // "small" // ] //}

for expected result utilize var array = new {"123", "aaa"};

c# asp.net-mvc json

No comments:

Post a Comment