应用场景:
a.在C#中创建类对象,但是不想创建类代码的时候
b.在C#后台代码中生成Json对象,供前台ajax调用时候
c.JavaScriptSerializer类生成Json对象的序列化和反序列化
代码实现:
using System.Web.Script.Serialization;
class _5new
{
public static void Main1()
{
var p = new { ID = 1, Name = "binfire", Password = "xx" };
Console.WriteLine(p.ID);
JavaScriptSerializer serializer = new JavaScriptSerializer();
string json=serializer.Serialize(p);
Console.WriteLine(json);
}
}