JSON to DataTable (method of regular expressions)

Source: Internet
Author: User

/// <summary>        ///convert JSON to DataTable/// </summary>        /// <param name= "Strjson" >the resulting JSON</param>        /// <returns></returns>        PrivateDataTable jsontodatatable (stringStrjson) {            //Convert JSON formatStrjson = Strjson.replace (",\"","*\""). Replace ("\":","\"#").            ToString (); //Remove Table name            varRG =NewRegex (@"(? <={) [^:]+ (? =:\[)", regexoptions.ignorecase); stringStrName =RG. Match (Strjson).            Value; DataTable TB=NULL; //Remove Table nameStrjson = strjson.substring (Strjson.indexof ("[") +1); Strjson= Strjson.substring (0, Strjson.indexof ("]")); //Get DataRG =NewRegex (@"(? <={) [^}]+ (? =})"); MatchCollection MC=RG.            Matches (Strjson);  for(inti =0; I < MC. Count; i++)            {                stringStrrow =Mc[i].                Value; string[] strrows = Strrow.split ('*'); //Create a table                if(TB = =NULL) {TB=NewDataTable (); Tb. TableName=StrName; foreach(stringStrinchstrrows) {                        varDC =NewDataColumn (); string[] Strcell = str. Split ('#'); if(strcell[0]. Substring (0,1) =="\"")                        {                            intA = strcell[0].                            Length; dc. ColumnName= strcell[0]. Substring (1A2); }                        Else{DC. ColumnName= strcell[0]; } TB.                    Columns.Add (DC); } TB.                AcceptChanges (); }                //Add ContentDataRow dr =TB.                NewRow ();  for(intR =0; R < Strrows.length; r++) {Dr[r]= Strrows[r]. Split ('#')[1]. Trim (). Replace (",",","). Replace (":",":"). Replace ("\"",""); } TB.                Rows.Add (DR); Tb.            AcceptChanges (); }            returnTB; }

Note: The JSON format should be standardized


For this format is not judged, if this is not translated correctly, to give price double quotation marks

[{"Age": $, "height": 170}]

JSON to DataTable (method of regular expressions)

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.