JSON serialization and deserialization in C #

Source: Internet
Author: User
Tags tojson

One, VS2008 or above version

<summary>
Sequence
</summary>
<typeparam name= "T" > Object class </typeparam>
<param name= "T" > class objects </param>
<returns>json string </returns>
public static string jsonserializer<t> (T t)
{
Serializes the object into JavaScript Object Notation (JSON) and deserializes the JSON data into objects.
DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (T));
Create a stream whose support store is memory
MemoryStream ms = new MemoryStream ();
Serializes the specified object into JavaScript Object Notation (JSON) data and writes the generated JSON to the stream
Ser. WriteObject (MS, T);
Write byte array conversion string
String Tojson = Encoding.UTF8.GetString (ms. ToArray ());
Close the stream
Ms. Close ();
Returns the JSON character
return Tojson;
}

<summary>
Deserialization
</summary>
<typeparam name= "T" > Object class </typeparam>
<param name= "Strjson" > JSON string to Deserialize </param>
<returns> return Objects </returns>
public static T jsondeserializer<t> (String Strjson)
{
Serializes objects into JavaScript Object Notation (JSON) and deserializes JSON data into objects
DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (T));
Create a stream whose support store is memory
MemoryStream ms = new MemoryStream (Encoding.UTF8.GetBytes (Strjson));
Reads the document stream in JSON (JavaScript object notation) format and returns the deserialized object.
T obj = (t) ser. ReadObject (MS);
Return
return obj;
}

Call Method:

private void Button1_Click (object sender, EventArgs e)
{
Student s = new student ();
list<student> stu = new list<student> ();
S.age = 11;
S.height = 12;
S.weight = 10;
S.name = "Tao";
Stu. ADD (s);
Student s2 = new Student ();
S2.age = 21;
S2.height = 22;
S2.weight = 20;
S2.name = "Jian";
Stu. ADD (S2);
String json = Jsonhelper.jsonserializer < list<student>> (Stu);
LISTBOX1.ITEMS.ADD (JSON);
TextBox1.Text = JSON;
}
private void Button2_Click (object sender, EventArgs e)
{
list<student> s = jsonhelper.jsondeserializer<list<student>> (TextBox1.Text);
LISTBOX1.ITEMS.ADD (S[0].name);
}

        private void Button4_Click (object sender, EventArgs e)
         {
            student s = New Student ();
            s.age = 11;
            s.height = 12;
            s.weight = 10;
            s.name = "tao222";
            String json = jsonhelper.jsonserializer< Student> (s);
            LISTBOX1.ITEMS.ADD (JSON);
            TextBox1.Text = json;
       }

        private void Button5_click (object sender, EventArgs e)
         {
            student s = Jsonhelper.jsondeserializer<student> (TextBox1.Text);
            listBox1.Items.Add (s.name);
       }

        private void Button3_Click (object sender, EventArgs e)
         {
            DataTable DT = getdatatable ();
            String json = jsonhelper.jsonserializer (DT) ;
            LISTBOX1.ITEMS.ADD (JSON);
            TextBox1.Text = json;
       }

private void Button6_click (object sender, EventArgs e)
{
DataTable dt = jsonhelper.jsondeserializer<datatable> (TextBox1.Text);
LISTBOX1.ITEMS.ADD (dt. rows[0]["name"]);
}

Second, the VS2005 version of the internet has a packaged third party is very convenient

private void Button1_Click (object sender, EventArgs e)
{
Student s = new student ();
list<student> stu = new list<student> ();
S.age = 11;
S.height = 12;
S.weight = 10;
S.name = "Tao";
Stu. ADD (s);
Student s2 = new Student ();
S2.age = 21;
S2.height = 22;
S2.weight = 20;
S2.name = "Jian";
Stu. ADD (S2);
String json = Jsonconvert.serializeobject (stu);
LISTBOX1.ITEMS.ADD (JSON);
TextBox1.Text = JSON;
}

        private void button2_click (object sender, EventArgs e)
         {
            list< student> s = jsonconvert.deserializeobject<list<student>> (TextBox1.Text);
            listBox1.Items.Add (s[0].name);
       }

        private void Button3_Click (object sender, EventArgs e)
         {
            student s = New Student ();
            s.age = 11;
            s.height = 12;
            s.weight = 10;
            s.name = "Tao";
            String json = Jsonconvert.serializeobject (s);
            LISTBOX1.ITEMS.ADD (JSON);
            TextBox1.Text = json;
       }

        private void Button4_Click (object sender, EventArgs e)
         {
            student s = Jsonconvert.deserializeobject<student> (TextBox1.Text);
            listBox1.Items.Add (s.age);
       }

        private void Button5_click (object sender, EventArgs e)
         {
            DataTable DT = getdatatable ();
            String json = Jsonconvert.serializeobject ( DT);
            LISTBOX1.ITEMS.ADD (JSON);
            TextBox1.Text = json;
       }

private void Button6_click (object sender, EventArgs e)
{
DataTable dt = jsonconvert.deserializeobject<datatable> (TextBox1.Text);
LISTBOX1.ITEMS.ADD (dt. rows[0]["name"]);
}

JSON serialization and deserialization in C #

Related Article

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.