2

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 jsonan actually array in JavaScript?

6
  • 2
    Just remove single quotes (') around JSON output: var arr = <%=json%>; Commented Mar 23, 2015 at 12:22
  • 1
    Doing that gives me a syntax error though? Commented Mar 23, 2015 at 12:25
  • 2
    First parse the json to make an object by using var JObject = JSON.parse(arr); Commented Mar 23, 2015 at 12:27
  • @Boxiom Most likely that's mean your json is invalid Commented Mar 23, 2015 at 12:31
  • This should help you : sitepoint.com/convert-json-string-array Commented Mar 23, 2015 at 12:31

3 Answers 3

4

You can use the JSON.parse method to parse the JSON string into an array:

var arr = JSON.parse('<%=json%>');

However, as JSON is a subset of JavaScript syntax, you can actually use the JSON as a JavaScript array literal:

var arr = <%=json%>;
Sign up to request clarification or add additional context in comments.

Comments

2

Take it as demo

Your variable arr will be rendered like this. This will be string. so to convert a string into JSON Object you need to parse that string. by using JSON.parse(arr)

var arr = '[ { "line":"Some text", "timestamp":"00:10" }, { "line":"Some text", "timestamp":"00:10" } ]';

var JObject= JSON.parse(arr);

Now you can access JObject[0].line

This will give you

"Some text"

You can take a look Here at Mozilla

1 Comment

What if the JSON string has a quote in one of the values?
2

We use MVC and in our views (which pass up models) we use a simple line whenever we want to convert the model into a javascript object:

<script type="text/javascript">
        $(document).ready(function () {
            MyObject.Initialize(@Html.Raw(Json.Encode(Model)));
        });
</script>

The key to all of that being:

@Html.Raw(Json.Encode(Model))

"Model" is what MVC passes up but you could very easily do something like this: (Razor syntax)

@{
   List<Caption> CaptionList = new List<Caption>();
   var obj = new Caption
   {
     line = "Some text"
     timestamp = "00:10"
   };
   CaptionList.Add(obj);
}

then...

<script type="text/javascript">
        $(document).ready(function () {
            MyObject.Initialize(@Html.Raw(Json.Encode(CaptionList)));
        });
</script>

I haven't tested this but it should work, or some variation of it, you may need to put "CaptionList" as a property in some parent object and pass that into Json.Encode.

You could do this instead, as well:

<script type="text/javascript">
        $(document).ready(function () {
            var myArray = @Html.Raw(Json.Encode(CaptionList)));
        });
</script>

Note that jQuery isn't necessary here, it was just a way to wrap it up in the ready function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.