.net ???????json?????.net??????????????
???????????? ???????[ 2016/7/12 11:35:36 ] ??????????????????? .NET
??????????????д????.net????????json?????????????????????????????????汾??jsonhelp????????????????????檔???汾??jsonhelp?д???????????json????????????????????????????????????????jsonhelp???????????????????????????ε????????????£?
JsonHelp.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Text.RegularExpressions;
namespace JsonTest
{
class JsonHelp
{
public JsonHelp()
{
//
// TODO: Add constructor logic here
//
}
/// <summary>
/// ????????л? JSON ?????
/// </summary>
/// <typeparam name="T">????????</typeparam>
/// <param name="obj">???????</param>
/// <returns>JSON?????</returns>
public static string GetJson<T>(T obj)
{
//??? ??????? System.ServiceModel.Web
/**
* ??????????????????System.Runtime.Serialization.Json; Json??????????
* */
DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(T));
using (MemoryStream ms = new MemoryStream())
{
json.WriteObject(ms?? obj);
string jsonString = Encoding.UTF8.GetString(ms.ToArray());
string pattern = @"\/Date((d+)+d+)\/";
MatchEvaluator matchEvaluator = new MatchEvaluator(ConvertJsonDateToDateString);
Regex reg = new Regex(pattern);
jsonString = reg.Replace(jsonString?? matchEvaluator);
return jsonString;
}
}
/// <summary>
/// ??JSON?????????????
/// </summary>
/// <typeparam name="T">????????</typeparam>
/// <param name="jsonString">JSON?????</param>
/// <returns>???????</returns>
public static T ParseFormJson<T>(string jsonString)
{
T obj = Activator.CreateInstance<T>();
string pattern = @"d{4}-d{2}-d{2}sd{2}:d{2}:d{2}";
MatchEvaluator matchEvaluator = new MatchEvaluator(ConvertDateStringToJsonDate);
Regex reg = new Regex(pattern);
jsonString = reg.Replace(jsonString?? matchEvaluator);
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)))
{
DataContractJsonSerializer dcj = new DataContractJsonSerializer(typeof(T));
return (T)dcj.ReadObject(ms);
}
}
/// <summary>
/// ??Json???л????????/Date(1304931520336+0800)/???????
/// </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>
/// ????????????Json???
/// </summary>
private static string ConvertDateStringToJsonDate(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;
}
}
}
???????????
using ConsoleApplication1.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
using (var db = new JooWMSContext())
{
var admin = db.Admins.Where(p => p.ID > 0).OrderBy(p => p.ID);
foreach (var ad in admin)
{
Console.WriteLine(JsonTest.JsonHelp.GetJson<ConsoleApplication1.Models.Admin>(ad));
}
Console.ReadKey();
}
}
}
}
????д?????????? ?????EF ?????????????????????EF Power Tools ????beta4 ?汾??
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11