手动将结果数据集转换为JSON [英] Manually converting result dataset to JSON
问题描述
我有DataReader,它保存来自存储过程caal的结果.结果包含两个字段...
I have DataReader that holds the results from a stored procedure caal. The results consist of two fields ...
UserID
UserName
通常我将这些结果绑定到ASP.NET dropdownlist控件...
Normally I bind these results to an ASP.NET dropdownlist control ...
ddlUserList.DataSource = rdr // rdr is the DataReader
ddlUserList.DataTextField = "UserName"
ddlUserList.DataValueField = "UserID"
ddlUserList.DataBind()
但是,我现在正在尝试使用jQuery AJAX完成相同的任务.我所坚持的是如何手动将DataReader中保存的数据集转换为JSON.倍数值如何分隔?这看起来正确吗?
However I am now trying to accomplish the same thing using jQuery AJAX. What I am stuck on is how to manually convert the dataset held in the DataReader to JSON. How are multiples values separated? Does this look correct?
{{"UserID":1, "UserName":"Bob"}, {"UserID":2, "UserName":"Sally"},{"UserID":3, "UserName":"Fred"}}
我意识到那里有诸如JSON.NET之类的库来处理序列化,但是我现在正处于学习阶段,并希望确保我从头至尾地了解所有内容.
I realize there are libraries out there such as JSON.NET to handle the serialization but I am in the learning stage now and want to make sure I understand everything from the bottom up.
推荐答案
Was wondering if you have tried using System.Web.Script.Serialization.JavaScriptSerializer library?
您可以在此查看Rick Stahl的博客: http://www.west-wind.com/weblog/posts/737584.aspx
You can look at Rick Stahl's blog on this: http://www.west-wind.com/weblog/posts/737584.aspx
或者您也可以执行类似创建方法的操作,该方法将从数据读取器中提取数据并将其放置在对象列表中. (请参见下面的代码).这些对象列表将使用JavaScriptSerializer库进行序列化.
Or you could also do something like create a method that will pull out data from the datareader and place it in a list of objects. (See code below). These list of object will be serialized using the JavaScriptSerializer library.
希望这会有所帮助!
public class User
{
public int UserId { get; set; }
public string UserName { get; set;}
}
public class DataLayer
{
public string GetUsers(string connString)
{
string result = null;
List<User> users = null;
// get data using SqlReader
using(var conn = new SqlConnection(connString))
{
using(var cmd = new SqlCommand{ Connection = conn, CommandText = "SELECT * FROM Users", CommandType = CommandType.Text })
{
conn.Open();
var reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
if(!reader.HasRows)
return null;
//convert data reader to a list of user objects
users = (List<User>)ConvertToList<User>(ref reader);
conn.Close();
}
}
//convert list of objects in list to json objects
var jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
result = jsonSerializer.Serialize(users);
return result;
}
public static IList<T> ConvertToList<T>(ref SqlDataReader reader)
{
IList<T> result = null;
if (reader.IsClosed)
return result;
result = new List<T>();
T item = default(T);
while (reader.Read())
{
//create item instance
item = (T)Activator.CreateInstance<T>();
//get class property members
var propertyItems = item.GetType().GetProperties();
//populate class property members with data from data reader
for (int ctr = 0; ctr < reader.FieldCount; ctr++)
{
if(reader.GetName(ctr) == propertyItems[ctr].Name)
propertyItems[ctr].SetValue(item, UtilsHelper.GetValue<string>(reader[ctr]), null);
}
//add item to list
result.Add(item);
}
reader.Close();
reader.Dispose();
return result;
}
}
这篇关于手动将结果数据集转换为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!