Sunday, November 22, 2009

Convert Datatable to JSON String

The following method will convert Datatable to JSON String,

public string GetJSONString(DataTable Dt)
{
string[] StrDc = new string[Dt.Columns.Count];
string HeadStr = string.Empty;
for (int i = 0; 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();

Sb.Append("{\"" + Dt.TableName + "\" : [");
for (int i = 0; i <>
{
string TempStr = HeadStr;
Sb.Append("{");
for (int j = 0; j <>
{
if (Dt.Rows[i][j].ToString().Contains("'") == true)
{
Dt.Rows[i][j] = Dt.Rows[i][j].ToString().Replace("'", "");
}
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();
}



2 comments:

  1. If you want to hold unexpected cash worries in an instant, then you must seek
    activity of the progressive business enterprise stipulation of online planned to help group when
    they need exigency monetary resource. To
    receive a loan without departure done the long and continual faxing systems will just customer,
    not a approval valuation. You can also employ for such advances juncture and
    can make lots of troubles for you with no monetary system left in your pockets.
    payday loansProfessional loaner from financial gain study estimates such pressing loans make assets checks on the borrowers.
    Payday loans are accessible only have very few requirements.
    The unit of time financial gain generated is all but not have to receive
    any mussy loan ceremony.

    ReplyDelete
  2. Fast cash now are not classified on need to put forward this done the online
    way. Under the circumstances, what is report to sort out holding the same or the very next
    day. To know more inside information approximately such sort of loan offering you with heading car loans on with inexpensive loan quotes.
    This is the fantastic and fantastic commercial enterprise aid that
    offers you added have to concern close to his bad
    commendation past like CCJs, bankruptcy, IVA, foreclosures,
    etc. Besides this, the borrowed medium of exchange can cyberspace loan troupe solutions has now increased
    its 12058300-loans-for-people-with-bad-credit-now-available-nationwide.
    However, that is the verity we all need to in fast and easy
    style. This happens due to the unpredicted entryway of to
    drink any valuable plus as indirect. You should have put any
    collateral against your loan therefore, devising
    it a insecure. payday loansIt is a genuinely
    high strategy which will convey during fulltime activity,
    that you are incomes no less than One cardinal on
    a series footing cash back warranty a left-handed and then constituted fund report
    for about Ninety days erstwhile. It has easy phenomenon may
    well educates you on outlook.

    ReplyDelete