62,196
社区成员
发帖
与我相关
我的任务
分享
public class JsonHandler : IHttpHandler
{
busers objbusers = new busers();
public void ProcessRequest(HttpContext context)
{
List<easyUI.Model.musers> userList=new List<easyUI.Model.musers>();
userList=objbusers.GetModelList(""); //获取一个类集合;
context.Response.ContentType = "application/json";
context.Response.Cache.SetCacheability(HttpCacheability.NoCache); //禁止浏览器的缓存作用;
System.Type type =userList.GetType(); //获取userList的类型
//序列化:
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(type);
serializer.WriteObject(context.Response.OutputStream, userList);
}
public bool IsReusable
{
get
{
return false;
}
}
}
1. $rs = mysql_query('select * from users');
2. $result = array();
3. while($row = mysql_fetch_object($rs)){
4. array_push($result, $row);
5. }
6.
7. echo json_encode($result);
public void ProcessRequest(HttpContext context)
{
List<easyUI.Model.musers> userList=new List<easyUI.Model.musers>();
userList=objbusers.GetModelList(""); //获取一个类集合;
var data = new System.Collections.Hashtable();
data["rows"] = userList;
data["total"] = userList.Count;
//Newtonsoft.Json.Serialization. //
Serialize(data); //找到到。。。命名空间已经引用Newtonsoft.Json.Serialization.
//var data = new System.Collections.Hashtable();
//data["rows"] = userList;
//data["total"] = userList.Count; //获取行数
//context.Response.ContentType = "application/json";
//context.Response.Cache.SetCacheability(HttpCacheability.NoCache); //禁止浏览器的缓存作用;
//System.Type type =data.GetType(); //获取userList的类型
////序列化:
//System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(type);
//serializer.WriteObject(context.Response.OutputStream, data);
}
public void ProcessRequest(HttpContext context)
{
List<easyUI.Model.musers> userList=new List<easyUI.Model.musers>();
userList=objbusers.GetModelList(""); //获取一个类集合;
var data = new System.Collections.Hashtable();
data["rows"] = userList;
data["total"] = userList.Count; //获取行数
context.Response.ContentType = "application/json";
context.Response.Cache.SetCacheability(HttpCacheability.NoCache); //禁止浏览器的缓存作用;
System.Type type =data.GetType(); //获取userList的类型
//序列化:
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(type);
serializer.WriteObject(context.Response.OutputStream, data);
}
【要怎样修改呢?】我这样改,运行就报错了:
Type 'System.Collections.Generic.List`1[[easyUI.Model.musers, easyUI.Model, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null]]' with data contract name 'ArrayOfmusers:http://schemas.datacontract.org/2004/07/easyUI.Model' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.