The example in this article describes how C # implements a collection to convert to JSON-formatted data. Share to everyone for your reference, specific as follows:
<summary>///DataTable converted to JSON format///</summary>///<param name= "DT" ></param>///<return s></returns> public static string Tojson (DataTable dt) {if (dt!= null && dt.
Rows.Count > 0) {StringBuilder jsonbuilder = new StringBuilder ();
Jsonbuilder.append ("{\");
Jsonbuilder.append ("Data");
Jsonbuilder.append ("\": ["); for (int i = 0; i < dt. Rows.Count;
i++) {jsonbuilder.append ("{"); for (int j = 0; j < dt.) Columns.count;
J + +) {jsonbuilder.append ("\"); Jsonbuilder.append (dt. COLUMNS[J].
ColumnName);
Jsonbuilder.append ("\": \ ""); Jsonbuilder.append (dt. Jsonstringfromat. ROWS[I][J].
ToString ()));
Jsonbuilder.append ("\", ");
} jsonbuilder.remove (jsonbuilder.length-1, 1);
Jsonbuilder.append ("},");
} jsonbuilder.remove (jsonbuilder.length-1, 1);
Jsonbuilder.append ("]");
Jsonbuilder.append ("}"); Return Jsonbuilder.tostRing ();
else {return ""; }///<summary>///enumerable to JSON///</summary>///<param name= "list" ></param>///<retu rns></returns> public static string Enumerabletojson (IEnumerable list) {StringBuilder json = new StringBuilder
();
int i = 0; if (list!= null) {JSON.
Append ("{\" data\ ": ["); foreach (var item in list) {JSON.
Append ("{"); propertyinfo[] Pi = Item. GetType ().
GetProperties ();
int j = 0; foreach (var p in pi) {if (J < pi). LENGTH-1) {json. Append ("\" + p.name.tostring () + "\": \ "" + Jsonstringfromat (p.getvalue (item, NULL).
ToString ()) + "\", "); } else {json. Append ("\" + p.name.tostring () + "\": \ "" + Jsonstringfromat (p.getvalue (item, NULL).
ToString ()) + "\" ");
} j + +;
} i++; Json.
Append ("},"); } JSON. Remove (JSON.
Length-1, 1); Json.
Append ("]}");
} if (I!= 0) Return JSON.
ToString ();
else return "";}
PS: This site also provides the following JSON-related tools to facilitate reference for everyone to use:
JSON translates into C # entity class tools online:
Http://tools.jb51.net/code/json2csharp
Online JSON code inspection, inspection, landscaping, formatting tools:
Http://tools.jb51.net/code/json
JSON Online formatting tool:
Http://tools.jb51.net/code/jsonformat
Online Xml/json Mutual Conversion tool:
Http://tools.jb51.net/code/xmljson
JSON code online Format/beautify/compress/edit/Convert tools:
Http://tools.jb51.net/code/jsoncodeformat
C Language Style/html/css/json code formatting landscaping Tools:
Http://tools.jb51.net/code/ccode_html_css_json
Read more about C # Interested readers can view the site topics: "C # form Operation Tips Summary", "C # Common control usage Tutorial", "WinForm Control Usage Summary", "C # Programming Thread Usage Skills summary", "C # Operation Excel Skills Summary", "C # Summary of operational skills in XML files, C # tutorial on data structure and algorithms, C # array operation techniques Summary, and C # Introduction to object-oriented Programming
I hope this article will help you with your C # programming.