C # Fundamentals---C # How to handle JSON strings

Source: Internet
Author: User

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 http://msdn.microsoft.com/en-us/library/ee191864 (v=vs.110). ASPX]; 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. 

{"Message": "OK", "Nu": "9356359685", "Ischeck": "1", "com": "Yuantong", "status": "$", "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 company revenue "," Ftime ":" 2014-08-31 19:17:41 "},{" time ":" 2014-08-28 23:44:26 "," context ":" Guangdong Province Shenzhen Hengdian 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 parses 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, the name needs to maintain aType [ignore case], followed by the field that will automatically convert the type, if non-conformance will throw an exception
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacetestjson{ Public classPostaldeliverymodel {Private stringMessage =string.        Empty; Private stringNu =string.        Empty; Privatelist<singaldata> data =NewList<singaldata>(); //the puclic name needs to be the same as the JSON string, but the case is ignored         Public stringMessage {Get{return  This. Message; Set{ This. Message =value;} }         Public stringNu {Get{return  This. Nu;} Set{ This. Nu =value;} }         PublicList<singaldata>Data {Get{return  This. Data;} Set{ This. data =value;} }    }     Public classSingaldata {PrivateDateTime time =System.DateTime.Now; Private stringContext =string.        Empty; PrivateDateTime Ftime =System.DateTime.Now;  PublicDateTime Time {Get{return  This. Time; Set{ This. Time =value;} }         PublicDateTime Ftime {Get{return  This. Ftime;} Set{ This. Ftime =value;} }         Public stringContext {Get{return  This. Context;} Set{ This. Context =value;} }    }}

2. You only need to invoke the method when the object is good:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.IO;usingSystem.Web.Script.Serialization;//the frame corresponding to this namespace is System.Web.Extensionsnamespacetestjson{ Public classProgram { Public Static voidMain (string[] args) {            stringJsonstr =NewStreamReader ("JsonData.txt").            ReadToEnd (); Postaldeliverymodel Mode=NewJavaScriptSerializer (). 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.

C # Fundamentals---C # How to handle JSON strings

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.