SoFunction
Updated on 2025-04-04

Explanation of various application examples of Json strings in C#

In the program, any things and information can be described and carried by objects. In addition to the more popular XML, there is also a simple and fast form of processing target information, which is the Json format.

First of all, the Json format has its own fixed format: for example, the person object can be simply represented in the form {"name":"xxxx","age":12,"sex":"male"}.

The description of Json format is as follows: first use {} to contain the object information to be expressed, use PropertyName: Value in {} to describe object properties, and at the same time, Json strings can be nested multiple layers. For example: Json data: [{PropertyName: Value,PropertyName: Value},{PropertyName: Value,PropertyName: Value}], Json nesting format: {PropertyName: Value,PropertyName: {PropertyName: Value,PropertyName: Value}}, theoretically, it can be nested infinitely, but it is recommended that the nesting should not exceed 3 layers.

After understanding the Json format, how do we use Json in C# and associate already implemented objects with Json. Give some namespaces first

using ;
using ;
using ;
using ;
using ;
using ;

It should be noted here that these namespaces are supported in .net3.5 and above, and references that must be added in the reference.

The main classes used here are as follows:

JsonSerializer ,StringWriter,StringReader ,JsonWriter,JsonConvert, DataContractJsonSerializer。

1. Implement the mutual conversion process of custom classes and Json:

 public class Person
  {
    public  Person()
    {
    }
    public Person(string Name, string Sex, int Age, string Address, PersonCharacter Character)
    {
       = Name;
       = Sex;
       = Age;
       = Address;
       = Character;
    }
    public string Name { get; set; }
    public string Sex { get; set; }
    public int Age { get; set; }
    public string Address { get; set; }
    public PersonCharacter Character { get; set; }
  }
  public class PersonCharacter
  {
    public string Daode { get; set; }
    public string Wenhua { get; set; }
    public string Xiuyang { get; set; }
  }
 public void ShowConvert()
    {
      Person person = new Person("lanar", "male", 24, "Shaanxi", new PersonCharacter());
      JsonSerializer serializer = new JsonSerializer();
StringWriter sw = new StringWriter();
(new JsonTextWriter(sw), person );
(().ToString());
StringReader sr = new StringReader(@"{""Name"":""ppp"", ""Age"":"12"}");
Person p1 = (Project)(new JsonTextReader(sr), typeof(Person));
(+ "=>" + );
}

2. Contract method:Implementation using the provided DataContractJsonSerializer or JsonReaderWriterFactory

 public class Person
   {
     public  Person()
     {
     }
     public Person(string Name, string Sex, int Age, string Address, PersonCharacter Character)
     {
        = Name;
        = Sex;
        = Age;
        = Address;
        = Character;
     }
     [DataMember]
     public string Name { get; set; }
     [DataMember]
     public string Sex { get; set; }
     [DataMember]
     public int Age { get; set; }
      [DataMember]
     public string Address { get; set; }
     [DataMember]
     public PersonCharacter Character { get; set; }
   }
   public class PersonCharacter
   {
     public string Daode { get; set; }
     public string Wenhua { get; set; }
     public string Xiuyang { get; set; }
   }
     public void ShowConvert()
     {
       Person person = new Person("Xu Zhanpeng", "male", , "Shaanxi", new PersonCharacter());
       Person p = new Person() { Name = "Four Holy Lands", Age = , Sex = "male", Character = new PersonCharacter() { Daode="sds", Wenhua="dasd", Xiuyang="zzz"} };
       DataContractJsonSerializer serializer = new DataContractJsonSerializer(());
       string jsonText;
       try
       {
         using (MemoryStream stream = new MemoryStream())
         {
           (stream, p);
           jsonText = (());
           (jsonText);
         }
         using (MemoryStream ms = new MemoryStream((jsonText)))
         {
           DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Person));
           Person p = (Person)(ms);
         }
       }
       catch (Exception ex )
       {
         throw new Exception(,ex);
       }
     }

The contract method must add relevant contract modifiers to the class and related attributes: [DataContract], [DataMember] No contract modifier symbols are required to be added inside the embedded object.

The above is just the most commonly used application method. For implementations with special needs, you can use the json conversion class implemented by third parties for processing. You can use the namespace to introduce a namespace to use the JavaScriptSerializer class to achieve simple serialization.