2

I have a C# List<String>, and I need to convert it to a JavaScript list of strings.

I'm currently stuck at:

System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer(); test = oSerializer.Serialize(tempString); 

In JavaScript, I can get it like this (test is a protected string variable)

var servervalue = '<%=test %>'; 

In the console I get

var servervalue = '["100000001","200000002","200000003","300000006","300000007"]'; 

and I need to get it without the single quotation marks, like this:

["100000001", "200000002", "200000003", "300000006", "300000007"]; 
1
  • You should use Newtsonsoft.Json, Microsoft actually recommends it over the built in serializer. Also, you could simply use a global or Session and assign the value, depending on your implementation. Not sure your goal.
    – Greg
    CommentedJul 13, 2015 at 14:07

2 Answers 2

2

If you don't want the list to be wrapped by ' quotes, you can easily fix this by removing the quotes that appear around the <%=test %> in the code.

var servervalue = <%=test%>; 
0
    0
    var servervalue = eval('<%=test %>'); 

    You also can use eval to change the string to Array

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.