C resolves Json to DateTable
- 2020-12-19 21:09:54
- OfStack
This article shows an example of how C# resolves Json to DateTable. Share to everybody for everybody reference. Specific implementation methods are as follows:
#region will Json Parsed into DateTable ///
/// will Json Parsed into DateTable .
/// Json Data formats such as : ///
{table:[{column1:1,column2:2,column3:3},{column1:1,column2:2,column3:3}]}
///
/// To parse the Json string
/// return DateTable public DataTable JsonToDataTable(string strJson)
{
//
Take out the name of the table var rg = new Regex(@(?<={)[^:]+(?=:[), RegexOptions.IgnoreCase);
string strName = rg.Match(strJson).Value; DataTable tb = null;
// Remove the name of the table strJson = strJson.Substring(strJson.IndexOf([) + 1);
strJson = strJson.Substring(0, strJson.IndexOf(]));
// To get the 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();
} // Increase the content of 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
Format as follows:
{
table: [
{
column1: 1,
column2: 2,
column3: 3
},
{
column1: 1,
column2: 2,
column3: 3
}
]
}
Such as:
[{Code:MetaDataId,Name:MetaDataId},{Code:MetadataCode,Name: Serial number },{Code:SolutionName,Name: The name of the }]
After formatting:
[
{
Code: MetaDataId,
Name: MetaDataId
},
{
Code: MetadataCode,
Name: Serial number
},
{
Code: SolutionName,
Name: The name of the
}
]
Hopefully this article has helped you with your C# programming.