Two solutions to the garbled problem in the format of JSON return time _ Practical tips

Source: Internet
Author: User

Foreword: This period of time has not written a blog, the first is that I'm implementing the stunning and operational implementation of the privilege series, because we've all said it before, so we don't say that again, so we know that when we use JSON to return the data, the format of the time is usually changed. Become some of the characters we do not know, so when we encounter these problems, how do we solve it, today I come to novel this solution.

. Finding problems

(1). As we said in the preamble, we return some inexplicable things when we write the JSON parsing time, so how do we solve the problem? I now have two ways to solve this problem, let's start with the idea.

(2). The first solution is that we use jquery to solve it, and we know that when the value is passed from JSON to the foreground for assignment, we can manipulate it so that it can be directly decompile to show up on the project.

(3). The second solution is that I directly use C # code to compile in the background and pass directly to the foreground display.

(4). So let's take a look at the format of the time passed over if we don't parse JSON as shown in the figure:

. front Desk using jquery solution

(1) If our front desk uses jquery to solve this problem, so the first thing we think about is how we parse the process, and of course we think of ourselves writing a JavaScript script to parse the process, of course, it's not written by me, it's written by someone else. The script code is as follows:

Copy Code code as follows:

by Han Guanlong

/**

* An extension to date that converts date to a string of the specified format

* Month (M), Day (d), 12 hours (h), 24 hours (h), minutes (m), seconds (s), Week (E), quarter (q) can be 1-2 placeholders

* Year (Y) can be 1-4 placeholders, milliseconds (S) can only use 1 placeholders (1-3-digit digits)

* Eg:

* (New Date ()). Pattern ("Yyyy-mm-dd hh:mm:ss. S ") ==> 2006-07-02 08:09:04.423

* (New Date ()). Pattern ("Yyyy-mm-dd E HH:mm:ss") ==> 2009-03-10 II 20:09:04

* (New Date ()). Pattern ("Yyyy-mm-dd EE hh:mm:ss") ==> 2009-03-10 Tuesday 08:09:04

* (New Date ()). Pattern ("Yyyy-mm-dd EEE hh:mm:ss") ==> 2009-03-10 Tuesday 08:09:04

* (New Date ()). Pattern ("yyyy-m-d h:m:s.s") ==> 2006-7-2 8:9:4.18

Use: (eval (\d+) \//gi, "New Date") (Value.replace). Pattern ("yyyy-m-d h:m:s.s");

*/

Date.prototype.pattern = function (FMT) {

var o = {

"m+": This.getmonth () + 1,//month

"D+": this.getdate (),//day

"h+": this.gethours ()% 12 = 0? 12:this.gethours ()% 12,//hour

"h+": this.gethours (),//hour

"m+": this.getminutes (),//min

"S+": This.getseconds (),//sec

"q+": Math.floor (This.getmonth () + 3)/3),//Quarter

"S": this.getmilliseconds ()//MS

};

var week = {

"0": "/u65e5",

"1": "/u4e00",

"2": "/u4e8c",

"3": "/u4e09",

"4": "/u56db",

"5": "/u4e94",

"6": "/U516D"

};

if (/(y+)/.test (FMT)) {

FMT = Fmt.replace (regexp.$1, (this.getfullyear () + ""). substr (4-regexp.$1.length));

}

if (/(e+)/.test (FMT)) {

FMT = Fmt.replace (regexp.$1, (Regexp.$1.length > 1)? (Regexp.$1.length > 2?) "/u661f/u671f": "/u5468"): "") + week[this.getday () + ""]);

}

For (var k in O) {

if (new RegExp ("+ K +") "). Test (FMT)) {

FMT = Fmt.replace (regexp.$1, (regexp.$1.length = = 1)? (O[k]): (("+ o[k]"). substr (("" + o[k). length));

}

}

return FMT;

}

(2) When we finish writing the above script, this time we need to use this script information in the page, then how do we use this information, of course we need to refer to this object first:

@* a reference to the date format *@

<script src= "@Url. Content (" ~/content/datapattern.js ") ></script>

(3) At this time when we have finished quoting, we are not the last step, then we need to set the format of the Easyui display data where we write the JSON parsing script, the code is as follows:
Copy Code code as follows:

FrozenColumns: [[

{field: ' CK ', checkbox:true},//Select

{title: ' Primary key ', field: ' ID ', width:40, sortable:true},//Primary key

{title: ' Role name ', field: ' Realname ', width:100, sortable:true},//login name

{title: ' Role type ', field: ' Categorycode ', width:100, sortable:true},//username

{title: ' Sort code ', field: ' Sortcode ', width:100, sortable:true},

{title: ' Creator ', field: ' CreateBy ', width:90, sortable:true},

{

Title: ' Creation Time ', field: ' Createon ', width:140, Sortable:true,

Formatter:function (value, row, index) {

Return (eval (\d+) \//gi, "New Date") (/\/date\) ("yyyy-m-d h:m:s");

}

},

{title: ' Last Modified person ', field: ' ModifiedBy ', width:90, sortable:true},

{title: ' Modify Time ', field: ' ModifiedOn ', width:140, sortable:true}

]],


(4) Finally we can look at the conversion after the thought as shown in the picture:

. The background uses base classes to resolve

(1) above we said the first method, so now we have the second method, the second method, we can see from the title, we use the background of the base class to achieve this effect, then we know that we in the previous blog, we have established a basecontroller base warehouse, We need to use the base warehouse here.

(2) in the base warehouse we have written the following methods, in this method we use some processing time objects, we can study for ourselves:
Copy Code code as follows:

<summary>

Returns the JSON string for the processed time

</summary>

<param name= "Date" ></param>

<returns></returns>

Public Contentresult jsondate (object Date)

{

var timeconverter = new Isodatetimeconverter {DateTimeFormat = "yyyy-mm-dd HH:mm:ss"};

Return Content (Jsonconvert.serializeobject (Date, formatting.indented, TimeConverter));

}


(3) Next we need to call this method in the controller of our implementation function to return the object, the role controller's code is as follows:
Copy Code code as follows:


<summary>

The information that gets the role is displayed in the Roles list

</summary>

<returns> returns JSON object for role information </returns>

Public ActionResult Getallroleinfos ()

{

A query that implements pagination for users and multiple criteria, rows represents the total number of pages, and page represents the current page

int pageIndex = request["page" = = null? 1:int. Parse (request["page"));

int pageSize = request["rows" = = null? 10:int. Parse (request["Rows"]);

String realname = request["Realname"];

Int? Enabled = request["Enabled" = = null? -1:int. Parse (request["Enabled"]);

Int? Categorycode = request["Categorycode"] = = null? -1:int. Parse (request["Categorycode"]);

int total = 0;

A method of encapsulating a business logic layer to handle the information of a multiple-condition query

Roleinfoquery roleinfo = new Roleinfoquery ()

{

PageIndex = PageIndex,

PageSize = PageSize,

Realname = Realname,

Enabled = enabled,

Categorycode = Categorycode,

Total = 0

};

var date = _roleinfo.loadsearchdate (Roleinfo);

Constructs a JSON object to return

var result = new {total = Roleinfo. Total, rows = date};

return jsondate (Result);

}

(4) Finally our front desk restores to the original state, does not have to change, realizes the function as shown:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.