String array not serialized to json correctly?
Trying to pass some JSON in my opinion.
In the controller:
var array = new string[] {"123", "AAA"};
string jsonArray = JsonConvert.SerializeObject(array);
ViewBag.JsonDataLabels = jsonArray;
In script view:
var jLabels = '@ViewBag.JsonDataLabels';
But this json seems to be somewhat invalid. For example, if I do this in a script:
var jLabels = '@ViewBag.JsonDataLabels';
alert(jLabels); // OUTPUT KO : ["123","AAA"]
var jLabels2 = ["AAA", "123"];
alert(jLabels2); // OUTPUTS OK : AAA, 123
Why is my JSON not valid?
+3
source to share
1 answer
Analyze this (sample from newtonsoft site)
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"
// ]
//}
use var array = new {"123", "AAA"} for your expected result;
0
source to share