The relationship between serialized JSON string and parsing JSON string entities

Source: Internet
Author: User

General use of an entity to parse a JSON collusion often exists in the following situations:

1. The entity attribute serialized into JSON is more than the Entity attribute parsing the JSON string.

2. Parsing the JSON string has more entity properties than serializing it into JSON.

3. The entity class serialized into the JSON string does not match the entity class name that parses the JSON string.

4. The attributes of the entity serialized into the JSON string are inconsistent with the parsing JSON string entity attribute type, but the names are identical.

5. Whether any one entity can parse the JSON string.

The following are examples of experiments:
1. The entity attribute serialized into JSON is more than the Entity attribute parsing the JSON string.

This is the entity type that is serialized into JSON.

   public class Grade    {public        list<student> TS {get; set;}    }
  public class Student    {public        int Stunumber {set; get;}        public string Phone {get; set;}        public int id{set;get;}        public string Name {set; get;}    }

Entities that parse the JSON:

public class Student    {public        int id{set;get;}        public string Name {set; get;}    }

Here is some of the code for the experiment:

static void Main (string[] args)        {            string Json = "{\" ts\ ": [{\" stunumber\ ": 100,\" id\ ": 0,\" name\ ": \" name_0\ ", \" Phone\ ": \" 15901062960\ "},{\" stunumber\ ": 101,\" id\ ": 1,\" name\ ": \" name_1\ ", \" phone\ ": \" 15901062961\ "}]}";            /* If the first step verifies that the JSON object string has more properties than the entity, it can parse *             /Grade gr0 = jsonhelper.deserializejsontoobject<grade> (Json);//First step verification             if (gr0! = null && gr0. Ts.        Count > 0)             {                 Console.WriteLine ("When the JSON string object is more than an entity, it can parse into a solid");             } Console.readkey ();         }

The results of the operation are as follows:

It is proved that it can be parsed correctly when serializing the entity attribute of the JSON string more than parsing the entity attribute of the JSON string.

2. Verify that the entity attributes that parse the JSON string are more than the entity attributes serialized into JSON can be parsed properly.

The JSON string is identical to the original, and the entity parsing the JSON string has several more properties than the original, as follows:

    public class Student    {public        int id{set;get;}        public string Name {set; get;}        public int Stunumber {set; get;}        public string Phone {get; set;}        public int Age {set; get;}        public string Address {get; set;}    

Some of the code for the experiment is as follows:


Grade gr0 = jsonhelper.deserializejsontoobject<grade> (Json);//The second step verifies if (gr0! = null && gr0. Ssi Count > 0) { if (gr0. Ts[0]. Address = = null && gr0. Ts[0]. Age = = 0) { Console.WriteLine ("When the entity attribute of the JSON string is less than the real entity, after parsing, the reference type of the real entity is NULL, the value type is 0");} }

The results of the operation are as follows:

The experiment proves that this JSON string can be parsed normally when the entity attribute serialized into JSON is less than the entity attribute that really parses the JSON string, but no entity reference type attribute is parsed to null, and the value type is parsed to 0.

3. The entity class serialized into the JSON string is inconsistent with the entity class name of the parse JSON string, in fact we can not reflect the original entity name from the JSON string, so we guess it is possible, but still verify.

A new type is added here to parse the JSON string above, and the new entity type is as follows:

public class Gradeother    {public        list<student> TS {get; set;}    }

The test code is as follows:

Grade gr0 = jsonhelper.deserializejsontoobject<grade> (Json);//Third Step verification            gradeother Gr1 = Jsonhelper.deserializejsontoobject<gradeother> (Json);            if (gr0! = null && gr0. Ssi Count > 0&&gr1!=null&&gr1. Ts. Count >0)            {                Console.WriteLine ("The original entity type is not the same as the new entity type name does not matter, also can parse normally");            }

Operation Result:

The experiment proves that the entity type serialized into JSON string is not related to parsing the entity type name inconsistency of JSON string, and it can be parsed normally.

4. The attributes of the entity serialized into the JSON string are inconsistent with the parsing JSON string entity attribute type, but the names are identical.

First, add a new type, change the gradeother entity, let it parse the object into a new type, and see what the result is. The following code is added and modified:

Add public   class Studentother    {public        int ID {set; get;}        public string Name {set; get;}    } Modify public  class Gradeother    {public        list<studentother> TS {get; set;}    }

The experiment code is as follows:

  Grade gr0 = jsonhelper.deserializejsontoobject<grade> (Json);//Fourth Step verification            gradeother Gr1 = Jsonhelper.deserializejsontoobject<gradeother> (Json);            if (gr0! = null && gr0. Ssi Count > 0&&gr1!=null&&gr1. Ts. Count >0)            {                Console.WriteLine ("The type of the original entity's attribute is not the same as the new entity type, and can parse normally");            }

The experiment proves that the attributes of the entities serialized into JSON string are inconsistent with the parsing JSON string entity attribute types, but the same name can be parsed normally.

5. The above proofs to us, any one entity can parse JSON string, but some can parse out values and some are empty, should not error. But is it true? Let's verify that the JSON string can be parsed by all types.

Here JSON string We also use the previous JSON string, we added a new entity type to parse the JSON string.

public class Gradetemp    {public        list<string> TS {get; set;}    }

The experiment code is as follows:

Try            {                gradetemp GT = jsonhelper.deserializejsontoobject<gradetemp> (Json);                if (GT! = null)                {                    Console.WriteLine ("Any entity can parse JSON");                    if (GT. TS! = null)                    {                        Console.WriteLine ("The process of parsing JSON does not judge the field type, only the name");}                }                else                {                    Console.WriteLine ("Not all entities can parse JSON");}            }            catch (Exception ex)            {                Console.WriteLine (ex. Message. ToString ());                Console.WriteLine ("Error when the type of the property is in conflict with the original Name field type");                           }

The experimental results are as follows:

The experiment proves that not one entity can parse the JSON string, and when the property name is the same, an error occurs when the type is incompatible.

Conclusion:

Parsing the entity properties of the JSON and serializing the entity properties of the JSON string does not affect the normal parsing (meaning that the corresponding property can parse successfully), but if the two entity names of the same attribute types are inconsistent, if they are compatible with each other (the Class A and Class B properties are not okay, The main is the corresponding property can be converted), but also no problem, if not compatible, then the resolution will be error.

Source code address: Https://yunpan.cn/cStbTkMNS4pSV Access password 6f08

If there is something wrong, you are welcome to criticize.

The relationship between serialized JSON string and parsing JSON string entities

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.