using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Text.RegularExpressions;
using System.Text;
namespace WebApplication1 {
public partial class WebForm2 : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
DataContractJsonSerializer JsonModel = new DataContractJsonSerializer(typeof(Person));
DataContractJsonSerializer JsonModels = new DataContractJsonSerializer(typeof(List<Person>));
MemoryStream MS1 = new MemoryStream();
Person Pone = new Person()
{ Age = , Name = "my", CreateTime = DateTime.Now.AddHours(), BisFinish = false };
Person Ptwo = new Person()
{ Age = , Name = "you", CreateTime = DateTime.Now.AddDays(), BisFinish = true };
JsonModel.WriteObject(MS1, Pone);
string JsonOnePerson = Encoding.UTF8.GetString(MS1.ToArray());
List<Person> Parr = new List<Person>();
Parr.Add(Pone); Parr.Add(Ptwo);
MemoryStream MS2 = new MemoryStream();
JsonModels.WriteObject(MS2, Parr);
string JsonPersons = Encoding.UTF8.GetString(MS2.ToArray());
ConvJsonDate(JsonPersons);
ConvJsonDate(JsonOnePerson);
Response.Write(string.Format("<input id=\"Hidden1\" type=\"hidden\" value='{0}' />",
ConvJsonDate(JsonPersons)));
}
}
public string ConvJsonDate(string str) {
Regex Reg = new Regex(@"\\/Date\((\d+)\+(\d+)\)\\/");
foreach (Match item in Reg.Matches(str)) {
string strOld = item.Value.Replace("\\/Date(", "").Replace("+0800)\\/", ""); string strNew;
DateTime DT = new DateTime(, , );
strNew = DT.AddMilliseconds(double.Parse(strOld)).ToLocalTime().ToString("yy-MM-dd HH:mm:ss");
str = str.Replace(item.Value, strNew);
}
return str;
}
}
public class Person {
public string Name { get; set; }
public int Age { get; set; }
public DateTime CreateTime { get; set; }
public bool BisFinish { get; set; }
}
}