2019년 3월 19일 화요일
C# - DataTable To JSON
[#1]
using System.Web.Script.Serialization;
public string DataTableToJSONWithJavaScriptSerializer(DataTable table)
{
JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
List < Dictionary < string, object >> parentRow = new List < Dictionary < string, object >> ();
Dictionary < string, object > childRow;
foreach(DataRow row in table.Rows)
{
childRow = new Dictionary < string, object > ();
foreach(DataColumn col in table.Columns)
{
childRow.Add(col.ColumnName, row[col]);
}
parentRow.Add(childRow);
}
return jsSerializer.Serialize(parentRow);
}
[#2]
using Newtonsoft.Json;
string json = JsonConvert.SerializeObject(dataSet, Formatting.Indented);
[#3]
public JsonResult GetRestUserFav() {
return Json(dt, JsonRequestBehavior.AllowGet);
}
[#4]
using System.Collections.Generic;
using System.Data;
public static class CommonExtensions
{
public static string ToJson(this DataTable value)
{
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
serializer.MaxJsonLength = 2147483647;
List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
Dictionary<string, object> row;
foreach (DataRow dr in value.Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in value.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
return serializer.Serialize(rows);
}
public static string ToJson(this DataSet value)
{
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
serializer.MaxJsonLength = 2147483647;
Dictionary<string, List<Dictionary<string, object>>> dsList = new Dictionary<string, List<Dictionary<string, object>>>();
List<Dictionary<string, object>> rows;
Dictionary<string, object> row;
foreach (DataTable dt in value.Tables)
{
rows = new List<Dictionary<string, object>>();
foreach (DataRow dr in dt.Rows)
{
row = new Dictionary<string, object>();
foreach (DataColumn col in dt.Columns)
{
row.Add(col.ColumnName, dr[col]);
}
rows.Add(row);
}
dsList.Add(dt.TableName, rows);
}
return serializer.Serialize(dsList);
}
}
피드 구독하기:
댓글 (Atom)
Flutter #0
[Flutter 교육] Dart vs JavaScript 타입 시스템 비교 1. 기본 타입 차이 숫자 타입 // Dart int integerNumber = 42; // 정수 double floatingPoint = 3.14; // 부...
-
11 SQL Client for Productive Database Administration & Development Working as a web developer or database administrator, often n...
-
JsonUtility 유니티 5.3 이상부터 JsonUtility를 지원한다. 외부 JSON 라이브러리가 필요 없다. < JsonUtility 멤버 함수 > FromJson : JSON을 오브젝트로 변환한다. 오브젝트를 생성...
댓글 없음:
댓글 쓰기