C # Implementation of the method of transforming the collection into JSON-formatted data _c# tutorial

Source: Internet
Author: User
Tags json

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.

Related Article

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.