简体   繁体   English

使用Javascript / JQuery解析自动生成的.NET日期对象

[英]Parsing a Auto-Generated .NET Date Object with Javascript/JQuery

There are some posts on this, but not an answer to this specific question. 这里有一些帖子,但不是这个具体问题的答案。

The server is returning this: "/Date(1304146800000)/" 服务器返回: "/Date(1304146800000)/"

I would like to not change the server-side code at all and instead parse the date that is included in the .Net generated JSON object. 我想根本不更改服务器端代码,而是解析.Net生成的JSON对象中包含的日期。 This doesn't seem that hard because it looks like it is almost there. 这似乎并不难,因为它看起来几乎就在那里。 Yet there doesn't seem to be a quick fix, at least in these forums. 然而,至少在这些论坛中似乎没有快速解决方案。

From previous posts it sounds like this can be done using REGEX but REGEX and I are old enemies that coldly stare at each other across the bar. 从之前的帖子中可以看出这可以使用REGEX完成,但REGEX和我是老对手,在酒吧里冷冷地凝视着对方。

Is this the only way? 这是唯一的方法吗? If so, can someone point me to a REGEX reference that is appropriate to this task? 如果是这样,有人可以指向我适合此任务的REGEX参考吗?

Regards, 问候,

Guido 圭多

The link from Robert is good, but we should strive to answer the question here, not to just post links. 罗伯特的链接很好,但我们应该努力回答这里的问题,而不仅仅是发布链接。

Here's a quick function that does what you need. 这是一个快速的功能,可以满足您的需求。 http://jsfiddle.net/Aaa6r/ http://jsfiddle.net/Aaa6r/

function deserializeDotNetDate(dateStr) {
  var matches = /\/Date\((\d*)\)\//.exec(dateStr);

  if(!matches) {
    return null;
  }

  return new Date( parseInt( matches[1] ) );
}

deserializeDotNetDate("/Date(1304146800000)/");

Since you're using jQuery I've extended its $.parseJSON() functionality so it's able to do this conversion for you automatically and transparently . 由于您正在使用jQuery,我已经扩展了它的$.parseJSON()功能,因此它可以自动,透明地为您进行此转换。

It doesn't convert only .net dates but ISO dates as well. 它不仅转换.net日期,也转换ISO日期。 ISO dates are supported by native JSON converters in all major browsers but they work only one way because JSON spec doesn't support date data type. 所有主流浏览器中的原生JSON转换器都支持ISO日期,但它们只能以一种方式工作,因为JSON规范不支持日期数据类型。

Read all the details (don't want to copy blog post content here because it would be too much) in my blog post and get the code as well. 我的博客文章中阅读所有细节(不想复制博客文章内容,因为它太多了)并获取代码。 The idea is still the same: change jQuery's default $.parseJSON() behaviour so it can detect .Net and ISO dates and converts them automatically when parsing JSON data. 这个想法仍然是相同的:更改jQuery的默认$.parseJSON()行为,以便它可以检测.Net和ISO日期,并在解析JSON数据时自动转换它们。 This way you don't have to traverse your parsed objects and convert dates manually. 这样您就不必遍历解析的对象并手动转换日期。

How it's used? 怎么用?

$.parseJSON(yourJSONstring, true);

See the additional variable? 看到附加变量? This makes sure that all your existing code works as expected without any change. 这可以确保所有现有代码按预期工作而不进行任何更改。 But if you do provide the additional parameter and set it to true it will detect dates and convert them accordingly. 但是,如果您确实提供了附加参数并将其设置为true ,它将检测日期并相应地转换它们。

Why is this solution better than manual conversion? 为什么这个解决方案比手动转换更好? ( suggested by Juan ) Juan建议

  1. Because you lower the risk of human factor of forgetting to convert some variable in your object tree (objects can be deep and wide) 因为你降低了人为因素忘记转换对象树中的某些变量的风险(对象可以是深度和宽度)
  2. Because your code is in development and if you change some server-side part that returns JSON to the client (rename variables, add new ones, remove existing etc.), you have to think of these manual conversions on the client side as well. 因为您的代码处于开发阶段,并且如果您更改了一些将JSON返回给客户端的服务器端部件(重命名变量,添加新变量,删除现有等),您还必须在客户端考虑这些手动转换。 If you do it automatically you don't have to think (or do anything) about it. 如果你自动完成,你不必考虑(或做任何事情)。

Two top reasons from the top of my head. 我头顶的两个主要原因。

When overriding jQuery functionality feels wrong 当重写jQuery功能时感觉不对

When you don't want to actually override existing $.parseJSON() functionality you can minimally change the code and rename the extension to $.parseJSONwithdates() and then always use your own function when parsing JSON. 当您不想实际覆盖现有的$.parseJSON()功能时,您可以最小化地更改代码并将扩展名重命名为$.parseJSONwithdates() ,然后在解析JSON时始终使用您自己的函数。 But you may have a problem when you set your Ajax calls to dataType: "json" which automatically calls the original parser. 但是,当您将Ajax调用设置为dataType: "json"时,您可能会遇到问题dataType: "json"会自动调用原始解析器。 If you use this setting you will have to override jQuery's existing functionality. 如果使用此设置,则必须覆盖jQuery的现有功能。

The good thing is also that you don't change the original jQuery library code file. 好处是你不要改变原来的jQuery库代码文件。 You put this extension in a separate file and use it at your own will. 您将此扩展名放在单独的文件中,并根据自己的意愿使用它。 Some pages may use it, others may not. 有些页面可能会使用它,有些页面可能不会。 But it's wise to use it everywhere otherwise you have the same problem of human factor with forgetting to include the extension. 但是在任何地方使用它都是明智的,否则你会遇到同样的人为因素问题而忘记包含扩展名。 Just include your extension in some global Javascript file (or master page/template) you may be using. 只需将您的扩展名包含在您可能正在使用的某个全局Javascript文件(或母版页/模板)中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM