Function code below
Public static DataTable JsonToDataTable4 (string strJson)
{
The DataTable DataTable=new DataTable ();//instantiate
The DataTable result;
Try
{
JavaScriptSerializer JavaScriptSerializer=new JavaScriptSerializer ();
JavaScriptSerializer. MaxJsonLength=Int32. MaxValue.//maximum values
ArrayList ArrayList=javaScriptSerializer. Deserialize
If (arrayList. Count & gt; 0)
{
//header
Int maxColCount=GetArrayListMaxKeysCount (arrayList);//to get maximum number of field lines
The foreach (Dictionary
{
If (a dictionary. Keys. Count
{
Result=dataTable;
return result;
}
//get the biggest Keys field collection as a header, avoid row fields do not match field
If (dataTable. Columns. The Count==0 & amp; & Dictionary. Keys. Count
{
Foreach (string current in the dictionary. Keys)
{
If (a dictionary [current]!=null)
{
If (a dictionary [current]. GetType ()==typeof (int))//avoid number is missing, if int data into a decimal
{
The dataTable. Columns. The Add (current, typeof (decimal));
}
The else
{
The dataTable. Columns. The Add (current, dictionary [current]. GetType ());
}
}
The else//when the data is null, the default for the string format
{
The dataTable. Columns. The Add (current, typeof (string));
}
}
break;
}
}
//for the table assignment assignment
The foreach (Dictionary
{
DataRow DataRow=dataTable. NewRow ();
Foreach (string current in the dictionary. Keys)
{
If (a dictionary [current]!=null)
DataRow [current]=dictionary [current];
}
The dataTable. Rows. The Add (dataRow);//loop line is added to the DataTable
The dataTable. AcceptChanges ();
}
}
}
Catch
{
Throw;
}
Result=dataTable;
return result;
}
Public static int GetArrayListMaxKeysCount (ArrayList sampleList)
{
Try
{
Int MaxKeysCout=0;
The foreach (Dictionary
{
Int temp=dictionary. Keys. Count
If (temp & gt; MaxKeysCout)
{
MaxKeysCout=temp;
}
}
Return MaxKeysCout;
}
The catch (Exception ex)
{
Throw the ex.
}
}
CodePudding user response:
The.net object is a list, and you the description is a json object instance (not the instance list), the concept itself is wrong,CodePudding user response:
The