使用SQL中的数据在C#中创建Json数组

时间:2016-05-26 13:39:45

标签: c# sql json

我必须从SQL获取数据并将其作为JSON发送到应用程序的前端。我在C#中并不擅长,所以我一直在忙着它。目前我得不到有效的JSON:

  {"name":"Perez","company":"test"}{"name":"Jespersen","company":"Codeparc"}

正如您所看到的那样,它不是一个数组。我怎样才能做到这一点? 我的代码:

 using System;
 using System.Data.SqlClient;
 using Newtonsoft.Json;

 namespace eltklt_webapp
{
//Create the Client Object
public class Client
{
    public string name;
    public string company;

}
public partial class getData : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["type"] == "clients")
        {
                // SQL Server Read Statement Sample
                using (SqlConnection conn = new SqlConnection(Gizmos.getConnectionString()))
            {
                SqlCommand command = new SqlCommand("select * from clients", conn);
                conn.Open();

                SqlDataReader reader = command.ExecuteReader();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        Client Client = new Client();
                        Client.name = reader["name"].ToString();
                        Client.company = reader["company"].ToString();
                        string jsonString = JsonConvert.SerializeObject(Client);
                        Response.Write(jsonString);
                    }
                }
            }
        Response.End();
        }//end if Clients
    }
}
}

4 个答案:

答案 0 :(得分:2)

您是否尝试将Client个对象放入数组或列表中并在列表中调用JsonConvert.SerializeObject

答案 1 :(得分:2)

protected void Page_Load(object sender, EventArgs e)
{
    if (Request.QueryString["type"] == "clients")
    {
            List<Client> clients=new List<Client>();
            // SQL Server Read Statement Sample
            using (SqlConnection conn = new SqlConnection(Gizmos.getConnectionString()))
        {
            SqlCommand command = new SqlCommand("select * from clients", conn);
            conn.Open();

            SqlDataReader reader = command.ExecuteReader();
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    Client Client = new Client();
                    Client.name = reader["name"].ToString();
                    Client.company = reader["company"].ToString();
                    clients.Add(Client);
                    //string jsonString =                     JsonConvert.SerializeObject(Client);

                }
            }
        }
      var jsonString = JsonConvert.SerializeObject(clients);
      Response.Write(jsonString);
    Response.End();
    }//end if Clients
}

希望这可以给你基本的想法。

答案 2 :(得分:0)

谢谢!加入

 List<Client> clients=new List<Client>();

执行while循环并使用以下命令将时间推送到客户端列表中:

                clients.Add(Client);

然后序列化它,解决了问题!感谢你们!

答案 3 :(得分:0)

如果您可以使用SQL 2016,则有更好的方法。

SELECT name, company from clients FOR JSON PATH

在SQL 2016中还有一个很好的框架可以简化新的JSON功能:https://github.com/ahhsoftware/EzAdoVSSolution