在前臺使用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.-創新互聯
轉載來于:http://m.newbst.com/article26/cepgjg.html
成都網站建設公司_創新互聯,為您提供定制開發、營銷型網站建設、靜態網站、品牌網站制作、網站收錄、網頁設計公司
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯