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 );