在前台使用JQuery和AJAX的时候经常会需要后台返回JSON数据,以前我的做法是直接拼成的,后来才知道.net3.5支持JSON了,今天特别做了几个例子方便不会的朋友。高手们就不要看了
创新互联公司是一家集网站建设,南皮企业网站建设,南皮品牌网站建设,网站定制,南皮网站建设报价,网络营销,网络优化,南皮网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。Namespace: System.Runtime.Serialization.Json
Assembly: System.ServiceModel.Web (in System.ServiceModel.Web.dll)
只支持.net3.5
例1:
Hashtable ht =new Hashtable();
ht.Add("A","值1");
ht.Add("B","值2");
DataContractJsonSerializer _json = new DataContractJsonSerializer(ht.GetType());
_json.WriteObject(Response.OutputStream, ht);
值为:[{"Key":"A","Value":"值1"},{"Key":"B","Value":"值2"}]
例2:
[Serializable]
public class Person
{
public string Name { get; set; }
public string Address { get; set; }
public string Tel { get; set; }
}
Person _Person = new Person();
_Person.Name = "江湖小子";
_Person.Address = "北京";
_Person.Tel = "010-10000000";
DataContractJsonSerializer _json = new DataContractJsonSerializer(_Person.GetType());
_json.WriteObject(Response.OutputStream, _Person);
值为:{"
k__BackingField":"北京","例3:
如果:{"
k__BackingField":"北京","DataContractJsonSerializer _json = new DataContractJsonSerializer(typeof(Person));
using (FileStream fs = File.OpenRead(@"D: empp.txt"))
{
Person p = _json.ReadObject(fs) as Person;
Response.Write(p.Name);//江湖小子
}
例4:
//上面大家会觉得奇怪为什么会出现k__BackingField之类的东东呢,其实是是[Serializable]造成的,我们可以用下面的方式处理
DataContractAttribute 类说明
[DataContract]
public class Person
{
[DataMember(Order=1)] // Order - 数据成员的序列化和反序列化的顺序
public string Name { get; set; }
[DataMember(Order = 2)]
public string Address { get; set; }
[DataMember(Order = 3)]
public string Tel { get; set; }
}
Person _Person = new Person();
_Person.Name = "江湖小子";
_Person.Address = "北京";
_Person.Tel = "010-10000000";
DataContractJsonSerializer _json = new DataContractJsonSerializer(typeof(Person));
_json.WriteObject(Response.OutputStream, _Person);
值为:{"Name":"江湖小子","Address":"北京","Tel":"010-10000000"}
例5:
Person _Person = new Person();
_Person.Name = "江湖小子";
_Person.Address = "北京";
_Person.Tel = "010-10000000";
DataContractJsonSerializer _json = new DataContractJsonSerializer(_Person.GetType());
// 序列化
MemoryStream ms = new MemoryStream();
_json.WriteObject(ms, _Person);
ms.Position = 0;
StreamReader sr = new StreamReader(ms);
string con = sr.ReadToEnd();
Response.Write(con);//{"Name":"江湖小子","Address":"北京","Tel":"010-10000000"}
// 反序列化
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(con);
MemoryStream ms2 = new MemoryStream(buffer);
Person _Person2 = _json.ReadObject(ms2) as Person;
Response.Write(_Person2.Name);//江湖小子
更好的方法:
public class Province
{
public int Id { get; set; }
public string Name { get; set; }
}
using System.Web.Script.Serialization;//System.Web.Extensions.dll
public static string ToJSON(object obj)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(obj);
}
Province p = new Province();
p.Id = 123;
p.Name = "北京";
Response.Write(ToJSON(p));//{"Id":123,"Name":"北京"}