C # parse json

Source: Internet
Author: User

Some time ago, a project called a web service of an External Cooperation Organization. The published methods and returned values were both json strings, and the returned results left me stuck for a long time.
 
Returned results: {"dataList": "[{\" HH \ ": \" 5001314065 \ ", \" HM \ ": \" 602 sihui \", \ "COUNT (*) \": \ "1 \"}, {\ "hh\": \ "5001301296 \", \ "HM \": \ "Ningbo guangsha Property Management Co., Ltd. \", \ "COUNT (*) \": \ "1 \"}] "," message ":" data retrieved successfully! "," Data ":" 1 "," success ":" 1 "}

If you don't take a closer look, you just get the deserialization,
 
The deserialization method is as follows:

View Code

1 private T Deserialize <T> (string sJson) where T: class
2 {
3 DataContractJsonSerializer ds = new DataContractJsonSerializer (typeof (T ));
4 MemoryStream MS = new MemoryStream (Encoding. UTF8.GetBytes (sJson ));
5 T obj = (T) ds. ReadObject (MS );
6
7 ms. Close ();
8 return obj;
9}

 
During the deserialization process, an exception is reported. After checking, it is found that the json string contains special characters such as COUNT (*) and the attribute cannot be found during the deserialization process, later, we communicated with each other to change COUNT (*) to SUMTIME for normal parsing,
 
Here, let's talk about my practice: declare a class StopTimeResult which contains four attributes that correspond to the json string, and then declare that a class StopTime contains three attributes, which correspond to the nested json string, the hydrogen target json string is deserialized into a StopTimeResult instance, and then the dataList attribute in StopTimeResult is deserialized into a List <StopTime>

As follows:
 

View Code 1 [DataContract]
2 public class StopTime
3 {
4 [DataMember]
5 public string HH {get; set ;}
6 [DataMember]
7 public string HM {get; set ;}
8 [DataMember]
9 public string SUM {get; set ;}
10}
 

 

View Code 1 [DataContract]
2 public class StopTimeResult
3 {
4 [DataMember]
5 public string dataList {get; set ;}
6 [DataMember]
7 public string message {get; set ;}
8 [DataMember]
9 public string success {get; set ;}
10 [DataMember]
11 public string data {get; set ;}
12}

View Code

1 StopTimeResult str = new OMSHelper (). Deserialize <StopTimeResult> (result );
2 List <StopTime> stopTimeList = new OMSHelper (). Deserialize <List <StopTime> (str. dataList );

 

 
 
 

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.