I have the following class in C#:
public class Caption
{
public string line;
public string timestamp;
}
I have created some Caption objects and added them to a List:
List<Caption> CaptionList = new List<Caption>();
var obj = new Caption
{
line = "Some text"
timestamp = "00:10"
};
CaptionList.Add(obj);
I then use Json.NET to serialize the list to Json, like this:
public string json = JsonConvert.SerializeObject(CaptionList);
Now I want to use this as a JavaScript array, so I tried this:
var arr = '<%=json%>';
I don't have much JavaScript knowledge, but it seems like arr
is now a string and not an array because json
is in fact a C# string. I tried accessing the objects by using arr[i].line
etc, but that doesn't work.
How do I make the json
an actually array in JavaScript?
'
) around JSON output:var arr = <%=json%>;
var JObject = JSON.parse(arr);
json
is invalid