C # parse Json into DateTable,

Source: Internet
Author: User

C # parse Json into DateTable,

# Region parses Json into DateTable ///
/// Parse Json into DateTable.
/// Json Data Format :///
{Table: [{column1: 1, column2: 2, column3: 3}, {column1: 1, column2: 2, column3: 3}]}
///
/// Json string to be parsed
/// Return DateTable public DataTable JsonToDataTable (string strJson)
{
//
Retrieve the table name var rg = new Regex (@(? <= {) [^:] + (? =: [), RegexOptions. IgnoreCase );
String strName = rg. Match (strJson). Value; DataTable tb = null;
// Remove the table name strJson = strJson. Substring (strJson. IndexOf ([) + 1 );
StrJson = strJson. Substring (0, strJson. IndexOf (]);
// Obtain data
Rg = new Regex (@(? <= {) [^}] + (? =}); MatchCollection mc = rg. Matches (strJson );
For (int I = 0; I <mc. Count; I ++ ){
String strRow = mc [I]. Value; string [] strRows = strRow. Split (',');
// Create a table if (tb = null) {tb = new DataTable ();
Tb. TableName = strName; foreach (string str in strRows)
{Var dc = new DataColumn ();
String [] strCell = str. Split (':');
Dc. ColumnName = strCell [0]. Replace (,);
Tb. Columns. Add (dc );}
Tb. AcceptChanges ();
} // Add DataRow dr = tb. NewRow ();
For (int j = 0; j <strRows. Length; j ++)
{Dr [j] = strRows [j]. Split (':') [1]. Replace (,
);
} Tb. Rows. Add (dr );
Tb. AcceptChanges ();
}
Return tb ;}
# Endregion

 

?
1  
?
1  

The format is as follows:

?
1234567891011121314 {    table: [        {            column1: 1,             column2: 2,             column3: 3        },         {            column1: 1,             column2: 2,             column3: 3        }    ]}

 

For example:

?
1 [{Code: MetaDataId, Name: MetaDataId}, {Code: MetadataCode, Name: No.}, {Code: SolutionName, Name: Name}]

After formatting:

?
1234567891011121314 [    {        Code: MetaDataId,         Name: MetaDataId    },     {        Code: MetadataCode,         Name: No.    },     {        Code: SolutionName,         Name: Name    }]

 

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.