Use examples:
Copy Code code as follows:
protected void Page_Load (object sender, EventArgs e)
{
Person p = new person
{
Name = "Wuchang",
Email = "Wuchangx@qq.com",
Lastactive = DateTime.Now,
ARR = new string[] {"arr1", "arr2"},
Lst = new list<string> (new string[] {"Lst1", "Lst2"})
};
String json = P.tojson ();
This. TextBox1.Text = JSON;
Person PP = json. Parsejson<person> ();
This. TextBox2.Text = pp. Tojson ();
}
Realize
Copy Code code as follows:
public static Class Jsonextension
{
public static string Tojson (This object obj)
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer (obj. GetType ());
using (MemoryStream ms = new MemoryStream ())
{
Serializer. WriteObject (MS, obj);
Return Encoding.Default.GetString (Ms. ToArray ());
}
}
public static T-parsejson<t> (This string str)
{
T obj = activator.createinstance<t> ();
using (MemoryStream ms = new MemoryStream (Encoding.Unicode.GetBytes (str))
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer (obj. GetType ());
Return (T) serializer. ReadObject (MS);
}
}
}