English 中文(简体)
如何将扼杀改为“个人目标”
原标题:How to convert string to JSON Object

timeline.js + MVC + Ajax + JSON

hello,

我在把扼杀变为Json目标时有问题。

我不得不在我的网页上展示时间表,我使用了时间线。 j 同样,我能够利用静态数据操作时间表,数据如下:

Static Data

// 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 
}

我现在发言。

alert(data);

[object Object],[object Object],[object Object]

但现在我必须显示亚洲开发银行的数据,因此,我要求履行以下关于控制员的职能。

GetTimeLine method on controller

public JsonResult GetTimeline()
{
      JsonResult jr = new JsonResult();
      var objtimeline = objEntities.Timelines.Where(tl => 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;
}

function to call controller method

jQuery.ajax({
    type: "POST",
    url: "@Url.Content("~/Student/GetTimeline")",
    success: function (result) {
        data = result;
    },
});
alert(data);

我 the following alert

[{ start : new Date(2012,2,11,0,0,0), content : Parents meeting },{ start : new Date(2012,2,15,0,0,0), content : Exam Meeting }];

因此,问题在于将扼杀物改为Json物体,

我认为,我怎么能够把从控制员返回的扼杀改成Json物体。

最佳回答

你的工作太困难。 让框架为你们服务。

public JsonResult GetTimeline()
{
     var timeline = objEntities.TimeLines.Where( tl => tl.StudentID == Sessions.StudentID )
                                         .ToList() //required due to Convert call
                                         .Select( tl => new
                                          {
                                               start = Convert.ToDateTime(tl.CalculatedDate),
                                               content = tl.Description
                                          });
     return Json( timeline, JsonRequestBehavior.AllowGet );
}

然后使用<条码>。 (因为你特别允许查阅)或具体规定<代码>数据表:json 。

$.getJSON(  @Url.Action("gettimeline","student") , function(data) {
     alert(data);
});
问题回答

您的服务器返回并不有效。 或者给你的服务器方代码,这或许是有效的。 In JSON:

  • All object keys must be in double quotes, not single quotes.
  • All strings must be in double quotes, not single quotes.
  • new Date(...) is not valid (JSON doesn t have any concept of dates).

我认为,你希望建立一个阵列(而不是一个阵列),并将之分配到<条码>jr.Data,然后让>bject<。 处理您的序列化(但我没有在ASP.net MVC中这样做)。

Once you have the server returning valid JSON, ensure that it s returning it with the correct Content-Type header (the value is application/json). When you do, jQuery will see that it s JSON and deserialize it into an object graph for you. If you can t or don t want to make your server return valid JSON, add dataType: json to the ajax call to force it.





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签