修改后的JSON字符串

时间:2013-12-03 09:42:35

标签: c# json

我从数据库表中返回一个列表。它的代码如下:

public List<Album> GetAlbums()
            {
                List<Album> Albums = new List<Album>();
                using (SqlConnection con = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["DefaultConnectionString"].ConnectionString))
                {
                    con.Open();
                    SqlCommand com = new SqlCommand("GetAlbums", con);
                    com.CommandType = CommandType.StoredProcedure;
                    SqlDataReader dr = com.ExecuteReader();
                    while (dr.Read())
                    {
                        Albums.Add(new Album { id = Convert.ToInt32(dr.GetValue(0)), name = dr.GetValue(1).ToString().Trim(), artist = dr.GetValue(2).ToString().Trim() });
                    }
                }
                return Albums;
            }

上面的函数返回一个JSON字符串,因为它是在一个宁静的WCF服务中写的。 JSON字符串的格式如下:

{"GetAlbumResult":[{"artist":"Kanjoliya","id":128,"name":"Gopal"},{"artist":"Kapoor","id":143,"name":"Lalit"},{"artist":"Ayachit","id":138,"name":"Madhukar"},{"artist":"Chouhan","id":142,"name":"Manish"}]}

我真正想要的是以下格式的字符串:

{"results":100,"GetAlbumResult":[{"artist":"Kanjoliya","id":128,"name":"Gopal"},{"artist":"Kapoor","id":143,"name":"Lalit"},{"artist":"Ayachit","id":138,"name":"Madhukar"},{"artist":"Chouhan","id":142,"name":"Manish"}]}

在上面的字符串中,“results”属性将从存储过程返回。我需要在gridview中进行分页。我在代码和/或sql过程中需要做哪些更改?当然我的程序只是一个select * from ....声明。

1 个答案:

答案 0 :(得分:0)

您需要在实现中返回类似这样的返回类型,以便结果将包含记录数,GetAlbumResult将具有来自过程的结果。您无需更改存储过程。

class Result
{
  public int results {get;set;}
  public List<Album> GetAlbumResult { get; set;}
}