JSON Conversion Tool class for dataset in. net

Source: Internet
Author: User
/**
  • * Method name: datasettojson beat1.0
  • * Parameter introduction:
  • * Ds-dataset |
  • * Jsonname-name of the root element of the JSON data |
  • * Parname-array of names in the dataset to be converted |
  • * This method is a test sample version, which is generated because of my project needs ,.
  • * It is initially intended to be made into a class library. datatable can be converted.
  • * Completion time: White Valentine's Day
  • **/
  • Private string datasettojson (Dataset ds, string jsonname, string [] parname)
  • {
  • Try
  • {
  • If (DS = NULL)
  • {
  • Return "dataset is null, so I can't do it to JSON! ";
  • }
  • If (jsonname. Length <1)
  • {
  • Return "you set the JSON name is wrong! ";
  • }
  • If (Ds. Tables [0]. Columns. Count <parname. length)
  • {
  • Return "you give the parname is bigger than dataset columns! ";
  • }
  • String josn = "{\" "+ jsonname + "\":[";
  • String temp = "";
  • For (Int J = 0; j <Ds. Tables [0]. Rows. Count; j ++)
  • {
  • Temp = temp + "{";
  • For (INT I = 0; I <parname. length; I ++)
  • {
  • Temp + = "\" "+ parname [I] +" \ ": \" "+ Ds. tables [0]. rows [J] [parname [I] + "\"";
  • If (I! = Parname. Length-1)
  • {
  • Temp = temp + ",";
  • }
  • }
  • Temp = temp + "},";
  • }
  • Josn = josn + temp + "]}";
  • Return josn;
  • }
  • Catch (exception ex)
  • {
  • Return "codeing is error ----" + ex. tostring ();
  • }
  • }
  • 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.