Examples of serialization and deserialization values of JSON in C #



Examples of serialization and deserialization values of JSON in C #


1. First, you need to quote newtonsoft Json. DLL file (the code refers to using newtonsoft. JSON and using newtonsoft. JSON. LINQ;)

2. Define two classes, person and_ Name, the code is as follows: (how to create JSON strings of multiple root nodes? You can define multiple nested classes. Refer to the following _nameclass)

private class Person
            public string Age { get; set; }// Age
            public string Weight { get; set; }// weight
            public string Height { get; set; }// height
            public string Sex { get; set; }// Gender

            public _ Name Name = new _ Name();// Name category (big name and small name)

        public class _Name
            public string FirstName { get; set; }// name
            public string SecondName { get; set; }// pet name



a. The serialization code is as follows (see the effect picture after the code)

private void button1_Click(object sender, EventArgs e)
            Person per = new Person();
            per. Name. Firstname = "Xiaobai";
            per. Name. Secondname = "Xiaohong";
            per.Age = "20";
            per.Weight = "80kg";
            per.Height = "168cm";
            per. Sex = "male";

            string js = JsonConvert. SerializeObject(per);// serialize

            textBox1.Text = js;

b. The deserialization code takes the value of the corresponding node as follows (see the effect picture below)

When deserializing, you can actually understand the JSON string as an XML document. Multiple curly braces are equivalent to the meaning from the main body of XML to the child node to the root node. Here, we take out the value of name, and we can see that there are two root node big names and small names defined below

        private void button2_Click(object sender, EventArgs e)
            var jobject = JObject.Parse(textBox1.Text);
            var weight = jobject["Name"].ToString();
            textBox1.Text = weight;

c. Get the value of the root node

We have taken out the value of the name node above. We can do a value taking operation for this name here

        private void button3_Click(object sender, EventArgs e)
            var jobject1 = JObject.Parse(textBox1.Text);
            var b = jobject1["FirstName"].ToString();
            textBox1.Text = b;


Recommended Today

Big data Hadoop — spark SQL + spark streaming

catalogue 1、 Spark SQL overview 2、 Sparksql version 1) Evolution of sparksql 2) Comparison between shark and sparksql 3)SparkSession 3、 RDD, dataframes and dataset 1) Relationship between the three 1)RDD 1. Core concept 2. RDD simple operation 3、RDD API 1)Transformation 2)Action 4. Actual operation 2)DataFrames 1. DSL style syntax operation 1) Dataframe creation 2. SQL […]