如何在JavaScript中使用Json [英] How to use Json in JavaScript
本文介绍了如何在JavaScript中使用Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好朋友,
我已经将数据表转换为Json并在客户端访问它.我对如何使用此Json Object感到困惑.
我想以表格格式显示结果
在这里粘贴我的代码:
Hello Friends,
I have converted datatable to Json and accessing it on clientside. I''m stuck of how to use this Json Object.
I want to display the result in table format
Here I paste my code:
[System.Web.Services.WebMethod()]
public static Dictionary<string,object> getWIPComments()
{
DataTable objDT = new DataTable();
objDT.TableName = "mydt";
try
{
StringBuilder strQuery = new StringBuilder();
strQuery.Append("select jobno,commenttext from tbl_workinprogress_comments where jobno in ( ");
strQuery.Append(" select jobno from tbl_planning P ");
strQuery.Append(" inner join tbl_mechanicalbrief on tbl_mechanicalbrief.mechanicalid= P.mechanicalid ");
strQuery.Append(" where statusid in (10))");
SqlConnection objCon = new SqlConnection("server=xxxx;Initial Catalog=xxxx;User ID=sa;Password=xxxx;pooling=''true''; Max Pool Size=200;Connect Timeout=2000000;enlist=false;");
SqlCommand objCmd = new SqlCommand();
objCmd.CommandText = strQuery.ToString();
objCmd.CommandType = CommandType.Text;
objCmd.Connection = objCon;
objCon.Open();
SqlDataAdapter objDA = new SqlDataAdapter("", objCon);
objDA.SelectCommand = objCmd;
objDA.Fill(objDT);
}
catch(Exception e) { }
return ToJson(objDT);
}
private static List<dictionary><string,>> RowsToDictonary(DataTable objDT)
{
List<dictionary><string,>> objs = new List<dictionary><string,>>();
foreach (DataRow dr in objDT.Rows)
{
Dictionary<string,> drow = new Dictionary<string,>();
for (int i = 0; i < objDT.Columns.Count; i++)
{
drow.Add(objDT.Columns[i].ColumnName, dr[i]);
}
objs.Add(drow);
}
return objs;
}
private static Dictionary<string,> ToJson(DataTable table)
{
Dictionary<string,> d = new Dictionary<string,>();
d.Add(table.TableName, RowsToDictonary(table));
return d;
}</dictionary></dictionary></dictionary>
有什么想法,如何在客户端以表格式表示此数据?
预先感谢.
Any idea''s, how to represent this data in table format on client side?
Thanks in advance.
推荐答案
^ ]
http://weblogs. asp.net/dwahlin/archive/2009/05/03/using-jquery-with-client-side-data-binding-templates.aspx [ http: //weblogs.asp.net/scottgu/archive/2010/05/07/jquery-templates-and-data-linking-and-microsoft-contributing-to-jquery.aspx [ http://api.jquery.com/jQuery.template/ [
http://stephenwalther.com/blog/archive/2010/11/30/an-introduction-to-jquery-templates.aspx[^]
http://weblogs.asp.net/dwahlin/archive/2009/05/03/using-jquery-with-client-side-data-binding-templates.aspx[^]
http://weblogs.asp.net/scottgu/archive/2010/05/07/jquery-templates-and-data-linking-and-microsoft-contributing-to-jquery.aspx[^]
http://api.jquery.com/jQuery.template/[^]
这是最终解决方案:
aspx.cs文件代码
Here is final Solution:
aspx.cs file code
private static string ToString(DataTable table)
{
StringBuilder sb = new StringBuilder();
StringBuilder sb1 = new StringBuilder();
JavaScriptSerializer js = new JavaScriptSerializer();
if (table != null)
{
if (table.Rows.Count > 0)
{
foreach (DataRow dr in table.Rows)
{
sb.Append("<table><tbody><tr><td>");
sb.Append((dr["jobno"]));
sb.Append("</td><td>");
sb.Append((dr["commenttext"]));
sb.Append("</td></tr></tbody></table>");
}
}
}
//js.Serialize(sb, sb1);
return sb.ToString();
}
aspx文件代码(带有AJAX代码的jQuery)
aspx file code (JQuery with AJAX Code)
.ajax({ 类型:发布", url:"WIPCommentsNotification.aspx/getWIPComments", 数据: "{}", dataType:"json", contentType:"application/json; charset = utf-8", 成功:功能(数据){ 尝试 { 数据= data.hasOwnProperty("d")吗? data.d:数据; 绑定(数据); } 抓住(e){ alert(错误!"); } }, 错误:功能(事件,请求,设置){ 警报(msg); } }); 函数Bind(msg){ //alert("Hello"); //var response = JSON.parse(msg); //alert(response); 尝试 { var table =< table id =" tblResult"border =" 1"style =" width:100%;>< thead>< tr>< th> JobNo</th> th&th; Comment< ;/th</tr>/thead< tbody>; 表+ =味精; table + =''</tbody></table>'';
.ajax({ type: "Post", url: "WIPCommentsNotification.aspx/getWIPComments", data: "{}", dataType: "json", contentType: "application/json; charset=utf-8", success: function(data) { try { data = data.hasOwnProperty("d") ? data.d : data; Bind(data); } catch (e) { alert("Error!!"); } }, error: function(event, request, settings) { alert(msg); } }); function Bind(msg) { //alert("Hello"); // var response = JSON.parse(msg); // alert(response); try { var table = "<table id="tblResult" border="1" style="width:100%;"><thead> <tr><th>JobNo</th> <th>Comment</th> </tr></thead> <tbody>"; table += msg; table += ''</tbody></table>'';
这篇关于如何在JavaScript中使用Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文