Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Two Solutions to the problem of garbled codes in the format of Json return time

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "two solutions to the problem of garbled code in the format of Json return time". In daily operation, I believe that many people have doubts about the two solutions to the problem of garbled code in the format of Json return time. The editor consulted all kinds of data and sorted out a simple and useful method of operation. I hope it will be helpful for you to answer the doubts of "two solutions to the garbled problem in the format of Json return time"! Next, please follow the editor to study!

I haven't written a blog in this period of time. First of all, I am implementing the outstanding features of the permission series and the implementation of operations. Because we have already talked about these things before, we will not repeat them. Then we know that when we use Json to return data, the format of time will generally change and become some characters that we do not know. So how do we solve these problems when we encounter them? today I'm going to write a novel about the solution to this.

. Find a problem

(1)。 As we said in the preface, we return something inexplicable when we write Json parsing time, so how do we solve this problem? I now have two ways to solve this problem, let's first talk about the train of thought.

(2)。 The first solution is that we use Jquery to solve the problem, and we know that when the value is passed from the Json to the foreground for assignment, we can manipulate it so that it can be decompiled directly and the time is displayed on the project.

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

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

. Jquery solution is used at the front desk.

(1) if we use Jquery at the front desk to solve this problem, the first thing we think about is how to parse the process. Of course, we think of writing a JavaScript script to parse the process. Of course, this script is not written by me, but written by someone else and used by ourselves. The script code is as follows:

The copy code is as follows:

/ / by Han Yinglong

/ * *

* extension to Date to convert Date to String in a specified format

* 1-2 placeholders can be used for month (M), day (d), 12 hours (h), 24 hours (H), minutes (m), seconds (s), week (E), quarter (Q)

* 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") = > 2006-07-02 08purl 09purl 04.423

* (new Date ()) .pattern ("yyyy-MM-dd E HH:mm:ss") = > 2009-03-10 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-28 purl 918

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 (), / / points

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

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

}

}

Return fmt

}

(2) when we have finished writing the above script, we need to use the information of the script on the page, so how do we use this information, of course, we need to refer to this object first:

@ * reference in date format * @

(3) at this time, when we have finished quoting, we are just short of the last step. At this time, we need to set the format of the easyUI display data where to call the script for Json parsing written above, as follows:

The copy 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: 'sorting 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 (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, we can take a look at the transformed thoughts, as shown in the figure:

. The background uses the base class to solve the problem.

(1) above we talked about the first method, so now let's talk about the second method. If we talk about the second method, we can see from the title that we use the background base class to achieve this effect. Then we know that we have established a BaseController-based repository in our previous blog, and we need to use the base repository here.

(2) in the base warehouse, we have written the following method. In this method, we have used some objects to deal with the time, so you can study it yourself:

The copy code is as follows:

/ / /

/ / return the Json string of the processed time

/ / /

/ / /

/ / /

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 our functional controller to return the object. The code of the role controller is as follows:

The copy code is as follows:

/ / /

/ / getting information about the role is displayed in the role list.

/ / /

/ / Json object that returns role information

Public ActionResult GetAllRoleInfos ()

{

/ / implement the paging query for users and multiple conditions. Rows indicates 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

/ / encapsulate the method of a business logic layer to deal with the information of multi-conditional queries

RoleInfoQuery roleinfo = new RoleInfoQuery ()

{

PageIndex = pageIndex

PageSize = pageSize

RealName = RealName

Enabled = Enabled

CategoryCode = CategoryCode

Total = 0

}

Var date = _ roleInfo.loadSearchDate (roleinfo)

/ / construct the Json object to return

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

Return JsonDate (result)

}

(4) finally, our foreground returns to its original state without change, and the functions are as shown in the figure:

At this point, the study of "two solutions to garbled problems in the format of Json return time" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report