在前台使用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);
值为:{"<Address>k__BackingField":"北京","<Name>k__BackingField":"江湖小子","<Tel>k__BackingField":"010-10000000"}
例3:
如果:{"<Address>k__BackingField":"北京","<Name>k__BackingField":"江湖小子","<Tel>k__BackingField":"010-10000000"}包含在文件中,可反序列化,文件需要是UTF-8格式
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":"北京"}
当前名称:c#生成JSON.-创新互联
转载来于:https://www.cdcxhl.com/article26/cepgjg.html
成都网站建设公司_创新互联,为您提供定制开发、营销型网站建设、静态网站、品牌网站制作、网站收录、网页设计公司
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联