SoFunction
Updated on 2025-03-06

C# implements Json string processing example

This article describes the C# implementation of Json string processing methods, and is shared with you for your reference. The specific analysis is as follows:

Generally, web application developers are very familiar with Json strings. In fact, in many requests, we return Json strings. So for how C# code handles Json strings, .Net encapsulates a class called JavaScriptSerializer[MSDN Library link:/en-us/library/ee191864(v=vs.110).aspx]; This class provides a method.

The following is a YTO Express information captured in express delivery 100. The information useful to us is the express delivery time and express delivery status. Then how should I do it?

{"message":"ok","nu":"9356359685","ischeck":"1","com":"yuantong","status":"200","condition":"F00","state":"3","data":[{"time":"2014-09-01 21:19:06","context":"Wuwei City Company in Gansu Province has signed up for it","ftime":"2014-09-01 21:19:06"},{"time":"2014-09-01 09:09:28","context":"The company in Wuwei, Gansu Province is sending the documents","ftime":"2014-09-01 09:09:28"},{"time":"2014-09-01 09:06:27","context":"Gansu Wuwei City Company has been received","ftime":"2014-09-01 09:06:27"},{"time":"2014-08-31 19:53:47","context":"The company in Lanzhou City, Gansu Province has issued ","ftime":"2014-08-31 19:53:47"},{"time":"2014-08-31 19:17:41","context":"Gansu Lanzhou City Company has been received","ftime":"2014-08-31 19:17:41"},{"time":"2014-08-28 23:44:26","context":"Henggang Company, Shenzhen, Guangdong Province, has been packaged","ftime":"2014-08-28 23:44:26"},{"time":"2014-08-28 23:19:12","context":"Henggang Company, Shenzhen, Guangdong Province, received the receipt","ftime":"2014-08-28 23:19:12"},{"time":"2014-08-28 21:55:35","context":"Henggang Company, Shenzhen, Guangdong Province, received the receipt","ftime":"2014-08-28 21:55:35"}]}

 
1. First analyze the Json string structure. Json{ message,nu,isCheck,Data{time,context,ftime}}; We first define a class, named PostalDeliveryModel. The structure of the class name needs to correspond to the Json structure, and the name needs to be kept the same [ignoring uppercase and uppercase]. Secondly, the corresponding fields are said to automatically convert the type. If the type does not match, an exception will be thrown.

using System;
using ;
using ;
using ;
using ;

namespace TestJson
{
  public class PostalDeliveryModel
  {
    private string message = ;
    private string nu = ;
    private List<SingalData> data = new List<SingalData>();

    // Public name needs to be the same as Json string, but case is ignored    public string Message 
    {
      get { return ; }
      set {  = value; }
    }

    public string Nu
    {
      get { return ; }
      set {  = value; }
    }

    public List<SingalData> Data
    {
      get { return ; }
      set {  = value; }
    }
  }

  public class SingalData
  {
    private DateTime time = ;
    private string context = ;
    private DateTime ftime = ;

    public DateTime Time
    {
      get { return ; }
      set {  = value; }
    }

    public DateTime FTime
    {
      get { return ; }
      set {  = value; }
    }

    public string Context
    {
      get { return ; }
      set {  = value; }
    }
  }
}

2. After the object is good, just call the method:

using System;
using ;
using ;
using ;
using ;
using ;
using ; // The corresponding framework of this namespace is
namespace TestJson
{
  public class Program
  {
    public static void Main(string[] args)
    {
      string jsonStr = new StreamReader("").ReadToEnd();
      PostalDeliveryModel mode = new JavaScriptSerializer().Deserialize<PostalDeliveryModel>(jsonStr);
      ();
    }
  }
}

3. Run the monitoring model object. The data is already inside the object.

4. Method review, although obtained. However, the Public property name of this method class must correspond to the Json string. I don’t know whether it can be mapped by adding [label] to the Public property. This way, you can customize the name and no longer need to be the same as the name in Json. Interested friends can study this further!

I hope this article will be helpful to everyone's C# programming learning.