json 格式:
{ "T_blog":
[
{"id":"14","title":"北京奥运开幕式","addtime":"2008-08-08"},
{"id":"15","title":"网络文化","addtime":"2008-09-12"},
{"id":"17","title":"北京下雨了","addtime":"2008-09-19"},
{"id":"21","title":"深圳地铁通了","addtime":"2008-09-25"}
]
}
1 思路: 遍历表格,将表中的数据按照 json格式 拼接;
public static string CreateJsonParameters(DataTable dt)
{
/**//**/
/**//* /****************************************************************************
* Without goingin to the depth of the functioning of this Method, i will try to give an overview
* As soon as this method gets a DataTable it starts to convert it into JSON String,
* it takes each row and in each row it grabs the cell name and its data.
* This kind of JSON is very usefull when developer have to have Column name of the .
* Values Can be Access on clien in this way. OBJ.HEAD[0].<ColumnName>
* NOTE: One negative point. by this method user will not be able to call any cell by its index.
* *************************************************************************/
StringBuilder JsonString = new StringBuilder();
//Exception Handling
if (dt != null && dt.Rows.Count > 0)
{
JsonString.Append("{ ");
JsonString.Append("\"T_blog\":[ ");
for (int i = 0; i < dt.Rows.Count; i++)
{
JsonString.Append("{ ");
for (int j = 0; j < dt.Columns.Count; j++)
{
if (j < dt.Columns.Count - 1)
{
JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\",");
}
else if (j == dt.Columns.Count - 1)
{
JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\"");
}
}
/**//**/
/**//*end Of String*/
if (i == dt.Rows.Count - 1)
{
JsonString.Append("} ");
}
else
{
JsonString.Append("}, ");
}
}
JsonString.Append("]}");
return JsonString.ToString();
}
else
{
return null;
}
}
2 这是一种各位先进的方式: json 类,对象的方法 将一个model对象序列化为json对象
using System.Web.Script.Serialization;
protected void LoadEts()
{
JavaScriptSerializer json = new JavaScriptSerializer();
DataTable ds_event = bll_event.GetAllList().Tables[0];
if (ds_event.Rows.Count > 0)
{
string str = string.Empty;
for (int i = 0; i < ds_event.Rows.Count; i++)
{
DataRow dr=ds_event.Rows[i];
Model.events oneevent = new Maticsoft.Model.events();
oneevent.id =int.Parse(dr["id"].ToString());
oneevent.title = dr["title"].ToString();
oneevent.username=dr["username"].ToString();
oneevent.pic=dr["pic"].ToString();
oneevent.address = dr["address"].ToString();
oneevent.cityaddress = dr["cityaddress"].ToString();
oneevent.starttime =DateTime.Parse(dr["starttime"].ToString());
oneevent.endtime =DateTime.Parse(dr["endtime"].ToString());
oneevent.contents = dr["contents"].ToString();
oneevent.classname = dr["classname"].ToString();
oneevent.seecount =int.Parse(dr["seecount"].ToString());
oneevent.joincount =int.Parse(dr["joincount"].ToString());
oneevent.jionmoney = dr["jionmoney"].ToString();
oneevent.personcount =int.Parse(dr["personcount"].ToString());
oneevent.tudu = dr["tudu"].ToString();
oneevent.viewcount =int.Parse(dr["viewcount"].ToString());
str += json.Serialize(oneevent);//对象 序列化的方法
}
this.Literal1.Text = str.ToString();
}
}