Data conversion format for JSON (DataTable or DataSet)

Source: Internet
Author: User

  1. <span style="FONT-SIZE:18PX;" >public class Datatableconvertjson
  2. {
  3. Convert #region DataTable into JSON format
  4. // <summary>
  5. // DataTable converted to JSON format
  6. // </summary>
  7. /// <param name= "DT" ></param>
  8. // <returns></returns>
  9. public static string Datatable2json (DataTable dt)
  10. {
  11. StringBuilder Jsonbuilder = new StringBuilder ();
  12. Jsonbuilder.append ("{\" ");
  13. Jsonbuilder.append (dt. TableName);
  14. Jsonbuilder.append ("\": [");
  15. Jsonbuilder.append ("[");
  16. For (int i = 0; i < dt. Rows.Count; i++)
  17. {
  18. Jsonbuilder.append ("{");
  19. For (int j = 0; j < dt. Columns.count; J + +)
  20. {
  21. Jsonbuilder.append ("\" ");
  22. Jsonbuilder.append (dt. COLUMNS[J]. ColumnName);
  23. Jsonbuilder.append ("\": \ "");
  24. Jsonbuilder.append (dt. ROWS[I][J]. ToString ());
  25. Jsonbuilder.append ("\", ");
  26. }
  27. Jsonbuilder.remove (jsonbuilder.length-1, 1);
  28. Jsonbuilder.append ("},");
  29. }
  30. Jsonbuilder.remove (jsonbuilder.length-1, 1);
  31. Jsonbuilder.append ("]");
  32. Jsonbuilder.append ("}");
  33. return jsonbuilder.tostring ();
  34. }
  35. Convert #endregion DataTable into JSON format
  36. #region DataSet into JSON format
  37. // <summary>
  38. /// DataSet converted to JSON format
  39. // </summary>
  40. /// <param name= "ds" >DataSet</param>
  41. // <returns></returns>
  42. public static string Dataset2json (DataSet ds)
  43. {
  44. StringBuilder JSON = new StringBuilder ();
  45. foreach (DataTable dt in ds. Tables)
  46. {
  47. Json.  Append ("{\" ");
  48. Json. Append (dt. TableName);
  49. Json.  Append ("\": ");
  50. Json. Append (Datatable2json (DT));
  51. Json.  Append ("}");
  52. } return JSON.  ToString ();
  53. }
  54. #endregion
  55. // <summary>
  56. // MSDN
  57. // </summary>
  58. /// <param name= "Jsonname" ></param>
  59. /// <param name= "DT" ></param>
  60. // <returns></returns>
  61. public static string Datatabletojson (string jsonname, DataTable dt)
  62. {
  63. StringBuilder Json = new StringBuilder ();
  64. Json.append ("{\" "+ jsonname + " \ ": [");
  65. if (dt. Rows.Count > 0)
  66. {
  67. For (int i = 0; i < dt. Rows.Count; i++)
  68. {
  69. Json.append ("{");
  70. For (int j = 0; j < dt. Columns.count; J + +)
  71. {
  72. Json.append ("\" "+ dt. COLUMNS[J]. Columnname.tostring () + "\": \ "" + dt. ROWS[I][J].  ToString () + "\" ");
  73. if (J < dt. COLUMNS.COUNT-1)
  74. {
  75. Json.append (",");
  76. }
  77. }
  78. Json.append ("}");
  79. if (i < dt. ROWS.COUNT-1)
  80. {
  81. Json.append (",");
  82. }
  83. }
  84. }
  85. Json.append ("]}");
  86. return json.tostring ();
  87. }
  88. }</span>

Data conversion format for JSON (DataTable or DataSet)

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.