JSON响应到HTML表

时间:2015-05-04 14:30:16

标签: jquery json html-table

我想将此信息从HTTPS GET请求显示为信息。

我想要展示的内容:

[{"id":"833",
  "vps_id":"1924",
  "event":"Your new virtual cloud server has been deployed",
  "timestamp":"2015-05-03 07:52:55","IP":"176.42.87.106"},
 {"id":"834",
  "vps_id":"1924",
  "event":"Power Cycle",
  "timestamp":"2015-05-03 07:53:30","IP":"176.31.67.189"}

2 个答案:

答案 0 :(得分:0)

在2行中有一种很酷的jquery方法可以做到这一点,但有时一种直接的方式更容易理解:

$.http({
  url: myCoolURL,
  method: "GET"
}).then(function (result) {

  var tableHTML = "<table><tr><th>id</th><th>vps id</th><th>event</th></tr>";  

  $.each(result.data, function() {
    tableHTML += "<tr>";
    tableHTML += "<td>"+this.id+"</td>";
    tableHTML += "<td>"+this.vps_id+"</td>";
    tableHTML += "<td>"+this.event+"</td>";
    tableHTML += "</tr>";
  });

  tableHTML += "</table>";
  $("#tablediv").innerHTML = tableHTML;
});

答案 1 :(得分:0)

我假设你的&#34;结果&#34; var存储一个这样的字符串:&#39; [{&#34; id&#34;:&#34; 833&#34;,&#34; vps_id&#34;:&#34; 1924&#34;,& #34; event&#34;:&#34;您的新虚拟云服务器已经部署&#34;,&#34;时间戳&#34;:&#34; 2015-05-03 07:52:55&#34; &#34; IP&#34;:&#34; 176.42.87.106&#34;},{&#34; ID&#34;:&#34; 834&#34;&#34; vps_id&#34; :&#34; 1924&#34;,&#34; event&#34;:&#34; Power Cycle&#34;,&#34; timestamp&#34;:&#34; 2015-05-03 07:53 :30&#34;&#34; IP&#34;:&#34; 176.31.67.189&#34;}]&#39;

如果我没错,结果是:

试试这个:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;



namespace CARSEZEE2015
{
    public partial class WebForm3: System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection("Data Source=server name;Initial Catalog=catalogname;User ID=id;Password=password");
        protected void Page_Load(object sender, EventArgs e)
        {
            con.Open();
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "Select * from DB";
            DataTable dt = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            DataList1.DataSource = dt;
            DataList1.DataBind();
            con.Close();
        }
'

请在此处查看示例:https://jsfiddle.net/mjb4t5z1/2/