Hello,
I have ChartJS that needs an array with double quoted values.
// What I need is:
var jsVarMonths = ["January","February","March","April","May","June","July","August","September","October","November","December"];
// What I get is:
var jsVarMonths = ["January\",\"February\",\"March\",\"April\",\"May\",\"June\",\"July\",\"August\",\"September\",\"October\",\"November\",\"December"];
Here’s my code:
[WebMethod]
public List<string> GetMonths()
{
try
{
var dq1 = @"""";
var dq2 = @"""";
var fdq = dq1 + "," + dq2;
var s = Months;
var replace = s.Replace(",", fdq);
var myArray = new List<string> {replace};
return myArray;
}
catch (Exception e)
{
Response.Write("<script>alert('" + Server.HtmlEncode(e.ToString()) + "')</script>");
throw;
}
}
I’ve been playing around a bit, so the code is a mess, but basically what I want is s.Replace(“,”, DOUBLE QUOTE + comma + DOUBLE QUOTE). For some reason, the debugger looks good, but the output keeps escaping with backslashes. Any thoughts on how to get rid of that backslash?
Maybe something I can do in the javascript? I tried to stringify and that didn’t work.
var serializerMth = new JavaScriptSerializer();
var monthsDeclaration = string.Format("var jsVarMonths = {0};", serializerMth.Serialize(GetMonths()));
Response.Write(monthsDeclaration);