Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to convert string to JSON Object
    text
    copied!<p><strong>timeline.js + MVC + Ajax + JSON</strong></p> <p>hello,</p> <p>I have problem while converting string to Json Object</p> <p>I have to display timeline on my webpage, I have used Timeline.js for the same, I am able to run the timeline using static data that is as following</p> <h1>Static Data</h1> <pre><code>// Create a JSON data table data = [ { 'start': new Date(2010, 7, 23), 'content': 'Conversation' }, { 'start': new Date(2010, 7, 23), 'content': 'New Conversation' }, { 'start': new Date(2010, 7, 23), 'content': 'Very New Conversation' } </code></pre> <p>now when I do </p> <pre><code>alert(data); </code></pre> <p>it gives me</p> <pre><code>[object Object],[object Object],[object Object] </code></pre> <p>but now I have to display a data from the DB, so I am calling the following function on controller</p> <h1>GetTimeLine method on controller</h1> <pre><code>public JsonResult GetTimeline() { JsonResult jr = new JsonResult(); var objtimeline = objEntities.Timelines.Where(tl =&gt; tl.StudentID == Sessions.StudentID).ToList().AsQueryable(); String newstr = "["; foreach(var tml in objtimeline) { DateTime date1 = Convert.ToDateTime(tml.CalculatedDate); newstr += "{'start': new Date("+date1.Year+","+date1.Month+","+date1.Day+","+date1.Hour+","+date1.Minute+","+date1.Second+"),'content':'"+tml.Description+"'},"; } newstr = newstr.TrimEnd(','); newstr += "];"; jr.Data = newstr; jr.JsonRequestBehavior = JsonRequestBehavior.AllowGet; return jr; } </code></pre> <h1>function to call controller method</h1> <pre><code>jQuery.ajax({ type: "POST", url: "@Url.Content("~/Student/GetTimeline")", success: function (result) { data = result; }, }); alert(data); </code></pre> <p>it gives me the following alert</p> <pre><code>[{'start': new Date(2012,2,11,0,0,0),'content':'Parents meeting'},{'start': new Date(2012,2,15,0,0,0),'content':'Exam Meeting'}]; </code></pre> <p>so the problem is with conversion of string to Json Object,</p> <p>How can I convert string returned from controller to Json Object on my view...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload