Home > Net >  [for] JSON conversion DataTable c # application scope
[for] JSON conversion DataTable c # application scope

Time:04-19

Run through the ArrayList ArrayList=javaScriptSerializer. Deserialize (strJson); Results the arrayList. Count=0 the should be how to modify, please input the Json for JsStr="{" code \ ": " 1001 \ ", \ \ "Id" : \ "81 ", \ "Name " : \ \ "test company"} ";//Json string


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 (strJson);

If (arrayList. Count & gt; 0)
{
//header
Int maxColCount=GetArrayListMaxKeysCount (arrayList);//to get maximum number of field lines
The foreach (Dictionary The dictionary in the arrayList)
{
If (a dictionary. Keys. Count ()==0)
{
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 ()==maxColCount)
{
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 The dictionary in the arrayList)
{
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 The dictionary in sampleList)
{
Int temp=dictionary. Keys. Count (a);
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
reference 1 floor interacting with professional developers
Response:
.net object is a list, and you the description is a json object instance (not the instance list), the concept itself is wrong,


Could you speak more straightforward simple some well
  •  Tags:  
  • C#
  • Related