好久没有做web了,JSON目前比较流行,闲得没事,所以动手试试将对象序列化为JSON字符(尽管DotNet Framework已经有现成的库,也有比较好的第三方开源库),而且只是实现了处理简单的类型,并且DateTime处理的也不专业,有兴趣的筒子可以扩展,代码比较简单,反序列化木有实现:( ,直接贴代码吧,都有注释了,所以废话不多说 :)
/// <summary>
/// Person dummy class
/// </summary>
public class Person
{
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
public string Address
{
get;
set;
}
private int h = 12;
public bool IsMarried
{
get;
set;
}
public string[] Names
{
get;
set;
}
public int[] Ages
{
get;
set;
}
public House MyHouse
{
get;
set;
}
public DateTime BirthDay
{
get;
set;
}
public List<string> Friends
{
get;
set;
}
public List<int> LoveNumbers
{
get;
set;
}
}
</div>
/// <summary>
/// Deserialize json string to object.
/// </summary>
/// <typeparam name="T">The type to be deserialized.</typeparam>
/// <param name="jsonString">json string.</param>
/// <returns>instance of type T.</returns>
T Deserialize<T>(string jsonString);
}
</div>
接口实现,还有待完善..