JSON anti-sequence session time format

Source: Internet
Author: User
Tags string to json

Using System;
Using System.Collections.Generic;
Using System.IO;
Using System.Linq;
Using System.Runtime.Serialization.Json;
Using System.Text;
Using System.Threading.Tasks;
Using System.Text.RegularExpressions;

Namespace Fjc.dbutility
{
public static Class Jsonherper
{

//<summary>
//JSON serialization
//</summary>
//<typeparam name= "T" ></typeparam>
//<param name= "T" ></PARAM>
///<RETURNS></RETURNS>
public static string Jsonserializer<t> (T t)
{
DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (T));
MemoryStream ms = new MemoryStream ();
Ser. WriteObject (MS, T);
String jsonstring = Encoding.UTF8.GetString (ms. ToArray ());
Ms. Close ();
//Replace the JSON Date string
String p = @ "\\/date\ ((\d+) \+\d+\) \\/";
MatchEvaluator matchevaluator = new MatchEvaluator (convertjsondatetodatestring);
Regex reg = new regex (p);
Jsonstring = Reg. Replace (jsonstring, MatchEvaluator);
return jsonstring;
}

//<summary>
//JSON deserialization
//</summary>
//<typeparam name= "T" ></typeparam>< br>//<param name= "jsonstring" ></PARAM>
//<returns></returns>
public static T Jsondeserialize<t> (String jsonstring)
{
//Convert the string "Yyyy-mm-dd HH:mm:ss" into "//date (1294499956278+0800)/ /"Format
String p = @" \d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2} ";
MatchEvaluator matchevaluator = new MatchEvaluator (convertdatestringtojsondate);
Regex reg = new regex (p);
Jsonstring = Reg. Replace (jsonstring, MatchEvaluator);
DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (T));
MemoryStream ms = new MemoryStream (Encoding.UTF8.GetBytes (jsonstring));
T obj = (t) ser. ReadObject (MS);
Return obj;
}

//<summary>
Convert JSON serialized time from/date (1294499956278+0800) to string
//</summary>
private static String convertjsondatetodatestring (Match m)
{
String result = String. Empty;
DateTime dt = new DateTime (1970, 1, 1);
dt = dt. Addmilliseconds (Long. Parse (M.groups[1]. Value));
dt = dt. ToLocalTime ();
result = dt. ToString ("Yyyy-mm-dd HH:mm:ss");
return result;
}
///<summary>
//Convert time string to JSON time
///</summary>
private static string convertdatestr Ingtojsondate (Match m)
{
String result = String. Empty;
DateTime dt = DateTime.Parse (M.groups[0]. Value);
dt = dt. ToUniversalTime ();
TimeSpan ts = dt-datetime.parse ("1970-01-01");
result = string. Format ("///date ({0}+0800)///", TS. TotalMilliseconds);
return result;
}


public static object Griddata (int Code, string Msg, Int. Total, String objects, String objects2)
{
var jsondata = new
//{
Total = Total,
Code = code,
msg = MSG,
Result = objects
//};

String jsonstr = "";

Jsonstr + = "{\" total\ ":" + "\" "+ Total +" \ ";

Jsonstr + = ", \" msg\ ":" + "\" "+ MSG +" \ ";

Jsonstr + = ", \" code\ ":" + "\" "+ Code +" \ ";

Jsonstr + = ", \" result\ ":" + objects;

if (!string. IsNullOrEmpty (OBJECTS2))
{
Jsonstr + = ", \" imglist\ ":" + objects2;
}

Jsonstr + = ", \" result\ ":" + objects;

Jsonstr + = "}";

return jsonstr;


}


<summary>
Convert tabs
</summary>
<param name= "Resultjson" ></param>
<returns></returns>
public static string Getjsonerl (String Resultjson)
{

String AA = Resultjson.replace ("\\u000d\\u000a", "\ r \ n");

return AA;
}


}


}

JSON anti-sequence session time format

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.