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