WORDPRESS 이것저것 파헤치기
  • about me
  • wordpress
    • theme
    • plugin
    • tip
  • programming
    • .net
    • sql
    • javascript
    • mobile
    • etc
  • reference
  • contact

Home » .net » DataTable 을 Json 형태로 간단히 사용하기에 좋은 함수.

Jun 18

DataTable 을 Json 형태로 간단히 사용하기에 좋은 함수.

By : admin/ Tags : .net, asp.net, Datatable, GetJSONString, Json/Category : .net, programming/0 Comment

DataTable 을 Json 형태로 간단히 사용하기에 좋은 함수.

*** .net 기준으로 ? ?System.Text.StringBuilder ?사용.


 

public static string GetJSONString(DataTable Dt, string PgTable)
{
string[] StrDc = new string[Dt.Columns.Count];

string HeadStr = string.Empty;
for (int i = 0; i < Dt.Columns.Count; i++)
{
StrDc[i] = Dt.Columns[i].Caption;
HeadStr += “\”” + StrDc[i] + “\” : \”” + StrDc[i] + i.ToString() + “¾” + “\”,”;
}

HeadStr = HeadStr.Substring(0, HeadStr.Length – 1);
StringBuilder Sb = new StringBuilder();

if (PgTable.Equals(“”))
{
Sb.Append(“{\”” + Dt.TableName + “\” : [“);
}
else
{
Sb.Append(“{\”” + PgTable + “\” : [“);
}

for (int i = 0; i < Dt.Rows.Count; i++)
{

string TempStr = HeadStr;

Sb.Append(“{“);
for (int j = 0; j < Dt.Columns.Count; j++)
{
TempStr = TempStr.Replace(Dt.Columns[j] + j.ToString() + “¾”, Dt.Rows[i][j].ToString());
}
Sb.Append(TempStr + “},”);

}
Sb = new StringBuilder(Sb.ToString().Substring(0, Sb.ToString().Length – 1));

Sb.Append(“]}”);
return Sb.ToString();
}

댓글 남기기 응답 취소

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다.

다음의 HTML 태그와 속성을 사용할 수 있습니다: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

그 밖의 기능

  • 등록하기
  • 로그인
  • 글 RSS
  • 댓글 RSS
  • WordPress.org

Service Title #1

Nullam dignissim convallis est. In hac habitasse platea dictumst. Ut aut reiciendis voluptatibus, tortor!

Service Title #2

Nullam dignissim convallis est. In hac habitasse platea dictumst. Ut aut reiciendis voluptatibus, tortor!

Service Title #3

Nullam dignissim convallis est. In hac habitasse platea dictumst. Ut aut reiciendis voluptatibus, tortor!

Service Title #4

Nullam dignissim convallis est. In hac habitasse platea dictumst. Ut aut reiciendis voluptatibus, tortor!

Copyright © 2025. All Rights Reserved. Powered by WordPress. Designed by MageeWP Themes