Two solutions for garbled characters in the format of Json return time

Source: Internet
Author: User

I have never written a blog during this time. First, I am implementing the unique features and operations of the permission series, because these things have been mentioned before, so we will not repeat this, so we know that when we use Json to return data, the time format will generally change to some characters that we don't know, so how can we solve these problems? Today I will introduce this solution.

. Problems Found

(1). As we said in the preface, we will return some inexplicable things When writing the Json parsing time. How can we solve this problem? I have two ways to solve this problem. Let's take a look at the ideas below.

(2 ). the first solution is to use Jquery. We know that when values are transferred from Json to the foreground for value assignment, we can operate on them, so that the decompilation time can be displayed on the project.

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

(4). Let's take a look at the format of the time passed if we haven't Parsed the Json:
 
. Use Jquery to solve the problem at the front end.

(1) If we use Jquery at the front end to solve this problem, we first think about how to resolve this process, of course, we thought of writing a JavaScript script to parse this process. Of course, this script is not written by me, but by someone else. The script code is as follows:
Copy codeThe Code is as follows:
// By Han Yinglong

/**

* Extended Date: converts Date to a String in the specified format.

* Month (M), Day (d), 12 hours (h), 24 hours (H), minute (m), second (s), Week (E), quarter (q) can use 1-2 placeholders

* Year (y) can use 1-4 placeholders, Millisecond (S) can only use 1 placeholder (1-3 digits)

* Eg:

* (New Date (). pattern ("yyyy-MM-dd hh: mm: ss. S") => 08:09:04. 423

* (New Date (). pattern ("yyyy-MM-dd e hh: mm: ss") => 20:09:04

* (New Date (). pattern ("yyyy-MM-dd EE hh: mm: ss") => 08:09:04 Tuesday

* (New Date (). pattern ("yyyy-MM-dd EEE hh: mm: ss") => 08:09:04 Tuesday

* (New Date (). pattern ("yyyy-M-d h: m: s. S") => 2006-7-2. 18

Use :( eval (value. replace (// Date \ (\ d +) \ // gi, "new Date ($1 )"))). 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 (), // minute

"S +": this. getSeconds (), // second

"Q +": Math. floor (this. getMonth () + 3)/3), // quarter

"S": this. getMilliseconds () // millisecond

};

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]): ("00" + o [k]). substr ("" + o [k]). length )));

}

}

Return fmt;

}

(2) After writing the above script, we need to use the script information on the page. Then, how can we use this information? Of course, we need to reference this object first:

@ * Date Format reference *@

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

(3) At this time, when the reference is complete, we will have to make the last step. At this time, we need to set the easyUI display data format where to call the Json parsing script we wrote above, the Code is as follows:
Copy codeThe Code is 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}, // User Name

{Title: 'sortcode', 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 (value. replace (// Date \ (\ d +) \ // gi, "new Date ($1 )"))). pattern ("yyyy-M-d h: m: s ");

}

},

{Title: 'Last modifier ', field: 'modifiedby', width: 90, sortable: true },

{Title: 'modification time', field: 'modifiedon', width: 140, sortable: true}

],


(4) Finally, let's take a look at what we thought after the conversion:
 
. The background uses the base class to solve the problem.

(1) The first method is mentioned above, so now we are talking about the second method. The second method is shown in the title, we use the background base class to achieve this effect, so we know that we have established a BaseController base repository in our previous blog, and we need to use the base repository here.

(2) In the base warehouse, we wrote the following method. In this method, we used some processing time objects. You can study them by yourself:
Copy codeThe Code is as follows:
/// <Summary>

/// Return the Json string of 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 code of the role controller is as follows:
Copy codeThe Code is as follows:

/// <Summary>

/// The role information is displayed in the role list.

/// </Summary>

/// <Returns> return the Json object of role information </returns>

Public ActionResult GetAllRoleInfos ()

{

// Implement the query of users and multi-condition pages. rows indicates the total number of pages and page indicates the current number of pages.

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;

// Encapsulate a business logic layer method to process multi-condition Query Information

RoleInfoQuery roleinfo = new RoleInfoQuery ()

{

PageIndex = pageIndex,

PageSize = pageSize,

RealName = RealName,

Enabled = Enabled,

CategoryCode = CategoryCode,

Total = 0

};

Var date = _ roleInfo. loadSearchDate (roleinfo );

// Construct a Json object to return

Var result = new {total = roleinfo. Total, rows = date };

Return JsonDate (result );

}

(4) At last, our front-end is restored to the original state without changing. The functions are as follows:
 

Related Article

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.