JSON strings are familiar to Web applications, but in many requests we return JSON strings. So how does the C # code handle the JSON string? NET encapsulates a class called Javascriptserializer[msdn Library link
]; This class provides a method.
Below this is I in express 100 to crawl of a flexible courier information. For our useful information is the courier time, courier status. Then how do I do it.
The code is as follows:
{"Message": "OK", "Nu": "9356359685″," "Ischeck": "1″," "com": "Yuantong", "status": "200″," condition ":" F00″, "state": "3″, "Data": [{"Time": "2014-09-01 21:19:06″," context ":" Gansu Province Wuwei Company has signed "," Ftime ":" 2014-09-01 21:19:06″},{"Time": 2014-09-01 09:09:28″, "context": "Wuwei Company in Gansu Province", "Ftime": "2014-09-01 09:09:28″},{" Time ": 2014-09-01 09:06:27″," context ":" Gansu Province Wuwei Company has revenue "," Ftime ":" 2014-09-01 09:06:27″},{"Time": "2014-08-31 19:53:47″," context ":" Lanzhou City, Gansu province, the company has issued "," Ftime ":" 2014-08-31 19:53:47″},{"Time": 2014-08-31 19:17:41″, "context": "Lanzhou City, Gansu province, the company has revenue", "Ftime": "2014-08-31 19:17:41″},{" time ":" 2014-08-28 23:44:26″, "context": "Guangdong province Shenzhen Heng Gang Company has been packaged", "Ftime": "2014-08-28 23:44:26″},{" Time ": 2014-08-28 23:19:12″," Context ":" Guangdong province Shenzhen Heng Gang Company has received "," Ftime ":" 2014-08-28 23:19:12″},{"Time": "2014-08-28 21:55:35″," context ":" Guangdong province Shenzhen Heng Gang Company has received ", "Ftime": "2014-08-28 21:55:35″}]}
1. First parse the JSON string structure. json{Message,nu,ischeck,data{time,context,ftime}}; We first define a class named Postaldeliverymodel, and the structure of the class name needs to correspond to the JSON structure, and the name needs to remain the same [ Ignore case], followed by the field that will automatically convert the type, if the type of non-conformance will throw an exception
The code is as follows:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Namespace Testjson
{
public class Postaldeliverymodel
{
private string message = String. Empty;
private string nu = String. Empty;
Private list<singaldata> data = new list<singaldata> ();
The puclic name needs to be the same as the JSON string, but the case is ignored
public string Message
{
get {return this.message;}
set {this.message = value;}
}
public string Nu
{
get {return this.nu;}
set {this.nu = value;}
}
Public list<singaldata> Data
{
get {return this.data;}
set {This.data = value;}
}
}
public class Singaldata
{
Private DateTime time = System.DateTime.Now;
private string context = string. Empty;
Private DateTime ftime = System.DateTime.Now;
Public DateTime Time
{
get {return this.time;}
set {this.time = value;}
}
Public DateTime Ftime
{
get {return this.ftime;}
set {this.ftime = value;}
}
public string Context
{
get {return this.context;}
set {This.context = value;}
}
}
}
2. You only need to invoke the method when the object is good:
The code is as follows:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Using System.IO;
Using System.Web.Script.Serialization; The frame corresponding to this namespace is System.Web.Extensions
Namespace Testjson
{
public class Program
{
public static void Main (string[] args)
{
String jsonstr = new StreamReader ("JsonData.txt"). ReadToEnd ();
Postaldeliverymodel mode = new JavaScriptSerializer (). Deserialize<postaldeliverymodel> (JSONSTR);
Console.readkey ();
}
}
}
3. Run the monitor model object. The data is already in the object.
4. Methodological review, although acquired. However, the public property name of this method class must correspond to the JSON string, and it is not known whether it can be mapped by adding [tag] to the public property, so that the name is no longer required to be the same as the JSON name. Ask other Daniel to point out the comments.
The above is for C # How to deal with the full contents of the JSON string, I hope you can like.
In addition to the Declaration,
Running GuestArticles are original, reproduced please link to the form of the address of this article
C # Processing JSON string Instance analysis
This address: http://www.paobuke.com/develop/c-develop/pbk23132.html
Related content C # example code using Litjson parsing JSON C # decimal Format Usage Summary Example C # program using LINQ to XML in querying XML format data C # methods for saving and reading DataTable information to XML format
C # Listing of all physical network adapters C # implementation Crawl HD beauty sister paper pictures comparison analysis of C # sorting algorithm using coercion type to implement conversion between strings and ASCII code in C #
C # Processing JSON string Instance analysis