Asp.net C# 把 Datatable转换成JSON 字符串

First of all, we have to fetch the records from the database (MS Sqlserver) into the C# DataTable, or we can add dynamic rows to our DataTable. So our code looks like as written below.

//*

)

{

;

;

;

;

;

;

;

;

;

;

;

;

}

方法一:Fetch each data (value), and append to our jsonString StringBuilder. This is how our code looks like

)

{

;

)

{

;

)

{

;

)

{

)

{

)

;

}

)

{

)

;

}

}

)

{

;

}

else

{

;

}

}

;

}

;

}

//*

方法 2: Convert DataTable to JSON using JavaScriptSerializer:

)

{

;

;

;

)

{

;

)

{

;

}

;

}

;

}

方法3(推荐): Convert DataTable to JSON using Json.Net DLL (Newtonsoft):

)

{

;

;

;

}

First of all, we have to fetch the records from the database (MS Sqlserver) into the C# DataTable, or we can add dynamic rows to our DataTable. So our code looks like as written below.

//*

)

{

;

;

;

;

;

;

;

;

;

;

;

;

}

方法一:Fetch each data (value), and append to our jsonString StringBuilder. This is how our code looks like

)

{

;

)

{

;

)

{

;

)

{

)

{

)

;

}

)

{

)

;

}

}

)

{

;

}

else

{

;

}

}

;

}

;

}

//*

方法 2: Convert DataTable to JSON using JavaScriptSerializer:

)

{

;

;

;

)

{

;

)

{

;

}

;

}

;

}

方法3(推荐): Convert DataTable to JSON using Json.Net DLL (Newtonsoft):

)

{

;

;

;

}