C # output json, which contains sub-json (which can contain infinitely sub-json,
The home page shows the method for converting DataTable to Json:
1 public static string TableToJson (DataTable dt) 2 {3 List <Dictionary <string, object> list = new List <Dictionary <string, object> (); 4 foreach (DataRow dr in dt. rows) // creates a new Dictionary <string, object> for each row, and adds each column of the row to the Dictionary 5 {6 Dictionary <string, object> result = new Dictionary <string, object> (); 7 foreach (DataColumn dc in dt. columns) 8 {9 result. add (dc. columnName, dr [dc]. toString (); 10} 11 list. add (result); 12} 13 return ToJson (list); // call the Serializer method 14}
1 /// <summary> 2 /// convert the set to json 3 /// </summary> 4 /// <param name = "d"> </param> 5 /// <returns> </returns> 6 public static string ToJson (object d) 7 {8 JavaScriptSerializer jss = new JavaScriptSerializer (); 9 return jss. serialize (d); 10}
Specific conversion instance:
1 # region get my order 2 public string GetMyOrders (string _ StrJson) 3 {4 StringBuilder sbStr = new StringBuilder (); 5 sbStr. append ("[{\" msg \ ": \" failed to be obtained. The parameter is incorrect. \ ", \" Data \ ": \" [] \ ", \" state \ ": \" 2 \ "}]"); 6 try 7 {8 CommonJsonModel oModel = new CommonJsonModel (Regex. replace (_ StrJson, @ "\ r \ n", ""); 9 List <CommonJsonModel> lst = oModel. getCollection (); 10 foreach (CommonJsonModel item in lst) 11 {12 // [{"_ Page": "1", "_ PageSize": "10 ", "_ Type": "1", "_ Status": "0", "_ LoginId": "15", "_ Risk Sion ": "390"}] 13 int Page = item. getValue ("_ Page") = ""? 1: Convert. ToInt32 (item. GetValue ("_ Page"); 14 int PageSize = item. GetValue ("_ PageSize") = ""? 10: Convert. ToInt32 (item. GetValue ("_ PageSize"); 15 int Type = item. GetValue ("_ Type") = ""? 0: Convert. ToInt32 (item. GetValue ("_ Type"); 16 int Status = item. GetValue ("_ Status") = ""? 0: Convert. ToInt32 (item. GetValue ("_ Status"); 17 int LoginId = item. GetValue ("_ LoginId") = ""? 0: Convert. toInt32 (item. getValue ("_ LoginId"); 18 string upload sion = item. getValue ("_ sion Sion"); 19 StringBuilder sbSqlWhere = new StringBuilder (); 20 sbSqlWhere. append ("PersonIsdel = 0"); 21 if (Status> 0) {sbSqlWhere. appendFormat ("and State = {0}", Status);} 22 if (LoginId> 0) {sbSqlWhere. appendFormat ("and AddPerson = {0}", LoginId);} 23 if (permission sion! = "") 24 {25 sbSqlWhere. appendFormat ("and BusinessId in (select ID from TB_Business where direction sion = {0})", direction sion); 26} 27 StringBuilder sbFields = new StringBuilder (); 28 sbFields. appendFormat ("*", Type); 29 DataSet ds = bll. getList ("TB_BusinessOrders", sbFields. toString (), "AddTime", PageSize, Page, false, true, sbSqlWhere. toString (); 30 if (ds! = Null & ds. Tables [0]. Rows. Count> 0) 31 {
// The first ableable32 DataTable dt = ds. Tables [0];
// Add a column sub-set 33 dt. columns. add ("OrdelDetailList"); 34 foreach (DataRow dr in dt. rows) 35 {36 dr ["OrdelDetailList"] = GetOrderDetailList (dr ["ID"]. toString (); 37} 38 sbStr. clear ();
// Convert the entire able to Json 39 string strTemp = EasyUIJsonHelper. TableToJson (ds. Tables [0]);
// Remove the Escape Character 40 strTemp = strTemp. replace ("\", ""); 41 strTemp = strTemp. replace ("\" [{"," [{"); 42 strTemp = strTemp. replace ("] \"} ","]} ");
// 43 sbStr. Append ("[{\" msg \ ": \" is obtained successfully. \ ", \" Data \ ":" + strTemp + ", \" state \ ": \" 0 \ "}]"); 44} 45 else46 {47 sbStr. clear (); 48 sbStr. append ("[{\" msg \ ": \" is obtained successfully. No matching data exists. \ ", \" Data \ ": \" [] \ ", \" state \ ": \" 0 \ "}]"); 49} 50} 51} 52 catch {} 53 return sbStr. toString (); 54} 55 56 private string GetOrderDetailList (string orderId) 57 {58 StringBuilder sbStr = new StringBuilder (); 59 DataSet ds = new ZhouFu. bll. TB_BusinessOrdersDetail (). getList ("OrderId =" + orderId); 60 if (ds! = Null & ds. tables [0]. rows. count> 0) 61 {62 DataTable dt = ds. tables [0]; 63 sbStr. clear (); 64 sbStr. append (EasyUIJsonHelper. tableToJson (dt); 65} 66 return sbStr. toString (); 67} 68 # endregion