c# - String array not serialized to json properly? -
Some JSON passes in my view.
In the Controller:
var array = new string [] {"123", "AAA"}; String jsonArray = JsonConvert.SerializeObject (array); ViewBag.JsonDataLabels = jsonArray;
In the script of the view:
var jLabels = '@ ViewBag.JsonDataLabels';
But this JSON seems to be somewhat unlawful if I do this in the script:
var jLabels = 'ViewBag.JsonDataLabels'; Warning (jLabels); // Output to: [& amp; Quot; 123 & amp; Quot ;, & amp; Quot; AAA & amp; Quot;] var jLabels2 = ["AAA", "123"]; Warning (jLabels2); // OKPUTUS is OK: AAA, 123
Why is my JSON invalid?
Analyze this (sample from NewtonSoft website)
Products Product = new product (); Product.Name = "Apple"; Product.Expiry = New DateTime (2008, 12, 28); Product. Size = new string [] {"small"}; String json = JsonConvert.SerializeObject (product); // {// "name": "Apple", // "end": "2008-12-28 T 00: 00: 00", // "size": [// "small" //] <}
var array = new {"123", "AAA"} for your expected result;
Comments
Post a Comment