c# 生成JSON .

在前台使用JQuery和AJAX的时候经常会需要后台返回JSON数据,以前我的做法是直接拼成的,后来才知道.net3.5支持JSON了,今天特别做了几个例子方便不会的朋友。高手们就不要看了 c 生成JSON - guigang520 - guigang520的博客

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:\temp\p.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":"北京"}

posted @ 2012-12-08 15:17  chinall  阅读(229)  评论(0编辑  收藏  举报