Implementation is required in recently developed PROJECTS. NET and Android programs do interface to implement Android Calling. net service!
What type of data is transmitted in the middle, because Android is good at JSON type parsing, so the final decision is to format the data in json,
Here are the helper classes that convert objects, collections, and so on to JSON strings!
1. First add an assembly reference
System.Web.Extensions
System.Runtime.Serialization
2. Introduce namespaces
Using System.Runtime.Serialization.Json;
Using System.Web.Script.Serialization;
3. Copy the following code into a new class named Jsonhelper
<summary>///json Helper class. Used to convert an object to a json-formatted string, or to transform a JSON string into an object. </summary> public static class Jsonhelper {///<summary>//convert object to JSON string// /</summary>//<typeparam name= "T" > Source Type </typeparam>//<param name= "obj" > Source Type instance < /param>//<returns>json strings </returns> public static string getjsonfromobj<t> (T Obj) {datacontractjsonserializer Jsonserializer = new DataContractJsonSerializer (obj. GetType ()); Using (memorystream ms = new MemoryStream ()) {jsonserializer.writeobject (ms, obj); return Encoding.UTF8.GetString (ms. ToArray ()); }}///<summary>//convert JSON string to Object///</summary>//<typeparam name= " T "> target type </typeparam>//<param name=" strjson ">json string </param>///<returns> target type one An instance of </returns> public static T getobjfromjson<t> (string strjson) {T obj = Activator.createinstanc E<t> (); Using (memorystream ms = new MemoryStream (Encoding.UTF8.GetBytes (strjson))) {datacontractjsonse Rializer Jsonserializer = new DataContractJsonSerializer (obj. GetType ()); return (T) Jsonserializer.readobject (ms); }}///<summary> convert DataTable to JSON String////</summary>//<param Nam e= "dt" > Data Sheet </param>//<returns>json string </returns> public static string Getjsonfromdatat Able (DataTable Dt) {StringBuilder jsonstring = new StringBuilder (); If (dt! = null && DT. Rows.Count > 0) {jsonstring.append ("{"); Jsonstring.append ("\" tableinfo\ ": ["); for (int i = 0; i < DT. rows.count; I++) {Jsonstring.append ("{"); for (int j = 0; J < Dt. columns.count; J + +) {if (j < DT. Columns.count-1) {jsonstring.append ("\" "+ dt.) Columns[j]. Columnname.tostring () + "\": "+" \ "" + DT. Rows[i][j]. ToString () + "\", "); } else if (j = = Dt. Columns.count-1) {jsonstring.append ("\" "+ dt.) Columns[j]. Columnname.tostring () + "\": "+" \ "" + DT. Rows[i][j]. ToString () + "\" "); }} if (i = = Dt. Rows.count-1) {jsonstring.append ("}"); } else {jsonstring.append ("},"); }} Jsonstring.append ("]}"); return jsonstring.tostring (); } else {return null; }}///<summary> convert object to JSON String///</summary>//<param name= "o BJ "> Source Object </param>//<returns>json data </returns> public static string Objtojson (this obj ECT Obj) {javascriptserializer serialize = new JavaScriptSerializer (); Return Serialize. Serialize (obj); }///<summary>//convert JSON string to Object///</summary>//<param name= "strjson" ; JSON strings </param>///<returns> target objects </returns> public static T jsontoobj<t> (string str Json) {javascriptserializer serialize = new JavaScriptSerializer (); Return Serialize. Deserialize<t> (strjson); }///<summary>//convert object to JSON string (control depth)///</summary>//<param name= "obj" > Source Objects </param>//<param name= "recursiondepth" > Depth </param>//<returns>json data </returns> public Stati C string Objtojson (this object obj, int Recursiondepth) {javascriptserializer serialize = new Javascrip Tserializer (); Serialize. Recursionlimit = recursiondepth; Return Serialize. Serialize (obj); }///<summary>//convert JSON string to object (control Depth)///</summary>//<param name= "strj Son ">json string </param>//<param name=" recursiondepth "> Depth </param>//<returns> Mesh Superscript object </returns> public static T jsontoobj<t> (string strjson, int Recursiondepth) {Java Scriptserializer serialize = new JavaScriptSerializer (); Serialize. Recursionlimit = recursiondepth; Return Serialize. Deserialize<t> (strjson); }///<summary> convert DataTable to JSON string///</summary> <param name= "dt" >DataTable</param>///<returns>json data </returns> public s Tatic string Datatabletojson (DataTable dt) {dictionary<string, object> dic = new Dictionary<st ring, object> (); int index = 0; foreach (DataRow Dr in Dt. Rows) {dictionary<string, object> result = new dictionary<string, object> (); foreach (DataColumn dc in DT. Columns) {result. ADD (dc. ColumnName, dr[dc]. ToString ()); } dic. ADD (index. ToString (), result); index++; } return Objtojson (dic); } }
JSON serialization Helper Class