Previously, the JSON serialization and deserialization was used in the project, and the instance used is now recorded for review. Please advise if there are any irregularities.
The ultimate goal of the code is to get a JSON string that meets resultclass<list<rtngetfindmycar>> requirements
JsonHelper.cs
public class Jsonhelper {//<summary& Gt JSON serialization///</summary> public static string jsonserializer<t> (T t) {DATAC Ontractjsonserializer ser = new DataContractJsonSerializer (typeof (T)); MemoryStream ms = new MemoryStream (); Ser. WriteObject (MS, T); String jsonstring = Encoding.UTF8.GetString (ms. ToArray ()); Ms. Close (); return jsonstring; }///<summary>//JSON deserialization///</summary> public static T JSONDESERIALIZE<T&G t; (string jsonstring) {DataContractJsonSerializer ser = new DataContractJsonSerializer (typeof (T)); MemoryStream ms = new MemoryStream (Encoding.UTF8.GetBytes (jsonstring)); T obj = (t) ser. ReadObject (MS); return obj; }}
Generic-Class Resultclass<t>
public class resultclass<t> {public bool issuccess; public int errorCode; public string errormsg; public string timestamp= DateTime.Now.ToString (); public T data; }
The principal data class, in which the class is added as a collection to the list<rtngetfindmycar>
public class Rtngetfindmycar {public string carnum = "";//License plate number public string parkingtimerange = "";// Parking Time Section public string parkingname = "";//parking lot name Public string zoneName = "";//parking Area name Public string dotno = "" ///parking lot number Public string mapPath = "";//Map path public string realtimecapture = "";//real-time snapshot }
The following is part of the main code of the function (which calls the WebService function of the co-manufacturer requires license plate information, so there is part of the code involved in the license plate)
resultclass<list<rtngetfindmycar>> rappdata = new Resultclass<list<rtngetfindmycar>> () ; Because the project needs to return the query information of the user's three vehicles at once, T in Resultclass<t> is a collection of vehicle information list<rtngetfindmycar>list< rtngetfindmycar> Listcar = new list<rtngetfindmycar> (); Instantiate the collection
//... The database query code that queries the user's 3 license plate numbers is omitted here, and the return value is the DS of the dataset type
DataRow Rowcar = ds. Tables[0]. rows[0];//The project database has agreed that the result data is only one row for (int i = 0; i < rowCar.ItemArray.Length; i++)//Cyclic call license plate {if (rowcar[i]. ToString (). Length > 0)//Ensure that the license plate is not empty {//... This eliminates the call to the partner manufacturer's WebService interface content, the return content is conforms to the resultclass<rtngetfindmycar> format JSON string
resultclass<rtngetfindmycar> datacls = new resultclass<rtngetfindmycar> (); Instantiate the result class Resultclass<rtngetfindmycar> To receive the results of a single license plate query DATACLS = jsonhelper.jsondeserialize<resultclass< Rtngetfindmycar>> (datatemp); The returned JSON content is deserialized as a whole, and the JSON string is converted to a JSON object Rtngetfindmycar rtnget = new Rtngetfindmycar (); if (datacls.issuccess = = true)//Determines whether the returned content is a successful flag bit, the item has a contract, true indicates that the returned data is trustworthy, false data is not trusted {string x = Jsonhelper.jsonseria Lizer<rtngetfindmycar> (Datacls.data); serialization, the Datacls object's data is converted to a string, the data part is actually the project needs the data Rtngetfindmycar class object Format Rtnget = jsonhelper.jsondeserialize< Rtngetfindmycar> (x);//deserialization, converting the data string to the object format of the Rtngetfindmycar class} listcar.add (Rtnget); Add rtnget that have been converted to the Rtngetfindmycar class object to list<rtngetfindmycar>}}//organize the final return value rappdata.issuccess = true; Rappdata.errormsg = Errcodeenum. The return value is normal. ToString (); rappdata.errorcode = (int) errcodeenum. The return value is normal; Rappdata.timestamp = DateTime.Now.ToString (); rappdata.data = listcar;//key, the data part of the return value is a list collection BAckdata = jsonhelper.jsonserializer<resultclass<list<rtngetfindmycar>>> (RAPPdata); Finally, the result class object containing the collection is converted to a JSON string for subsequent code use
Examples of JSON serialization and deserialization