Q:

How to loop through ViewData with Javascript in ASPX View

I have populated ViewData item with an array of questions so that I can store the json data in local storage:
string[] jsonQuestions = new string[StudentExam. QuestionCount];

            for (int i = 0; i < StudentExam. QuestionCount; i++)
            {
                jsonQuestions[i] = new JavaScriptSerializer().Serialize(StudentExam. QuestionsE[i]);

                ViewData[i. ToString()] = jsonQuestions[i];
            }

During testing I loaded the ViewData into the localstorage like this (hard coded) for the exact amount of questions:
            localStorage.setItem("obj0", JSON.stringify(<%=ViewData["0"]%>));
             localStorage.setItem("obj1", JSON.stringify(<%=ViewData["1"]%>));
             localStorage.setItem("obj2", JSON.stringify(<%=ViewData["2"]%>));
             localStorage.setItem("obj3", JSON.stringify(<%=ViewData["3"]%>));
             //...etc. 

In the view I am trying to now dynamically load the ViewData into the browser localstorage like this using javascript:
<script>

         var mod = "<%=Html. Raw(Model. TotalNumberOfQuestionsForTest. ToString())%>";

         for (var i = 0; i < mod; i++) {

             localStorage.setItem(
                 "obj" + i.toString(),
                 JSON.stringify(<%=ViewData[i.toString()]%>));

         };
    </script>

Now this gave me an error that it cannot find i, since we "escaped" the client side code.  So I changed it to :
for (var i = 0; i < mod; i++) {

             var s = <%=ViewData[%>i.toString()<%]%>;

             localStorage.setItem(
                 "obj" + i.toString(),
                 JSON.stringify(<%=ViewData[%>i.toString()<%]%>));

         };

This is the part where I'm stuck at.  I keep getting these errors on this line:
JSON.stringify(<%=ViewData[%>i.toString()<%]%>));

Error 7   Invalid expression term ';'

and

Error 8   ; expected

and when I run the web app this is all I get on inspect element:

Compilation Error
Description: An error occurred during the compilation of a resource
  required to service this request.  Please review the following specific
  error details and modify your source code appropriately.  
Compiler Error Message: CS1525: Invalid expression term ')'
Source Error:
Line 278:             localStorage.setItem( Line 279:
  "obj" + i.toString(), Line 280:
  JSON.stringify(<%=ViewData[%>i.toString()<%]%>)); Line 281: Line 282:
  };

Is there a way to escape the <%%> tags, into the javascript again for the javascript variable to use in the ViewData?  Or another way to do this? 
UPDATE:
I went on and played around with the code a bit more and this is the closest I have gotten to what I need to happen by doing it like this:
$(function () {

             <% for (int i = 0; i < 1; i++){%>
         <%="localStorage.setItem('obj" + i.ToString() + "',JSON.stringify('"%><%=ViewData[i. ToString()]%>"'))"

            <%}%>

         });

So this does loop through my questions and put the value in where I needed, but the last issue I have is that now by having to put the javascript in quotations :
<%="localStorage.setItem('obj" + i.ToString() + "',JSON.stringify('"%><%=ViewData[i. ToString()]%>"'))"

The last quotation mark is needed to close the string between the <%%> tags.  but this quotation mark is giving me the error:

Invalid or unexpected token

as my javascript to store the localstorage looks like this now:
localStorage.setItem("obj1", JSON.stringify("my json query"))" //this quotation is the problem

Any assistance or help will be greatly appreciated. 

A:

I finally fix the problem.  The issue with the quotation was that I was "stringifying" a string and the moment I removed the single quotation marks from the code:
$(function() {    
            <% for (int i = 0; i < Model. TotalNumberOfQuestionsForTest; i++){%>
            <%="localStorage.setItem('obj" + i.ToString() + "',JSON.stringify("%>
            <%=ViewData[i. ToString()]%>
            <%="))"%>
           <%}%>

The json object was successfully stored in the localstorage of the browser.