在ajax的已不请求中,常常返回json对象。可以利用json.net给我们提供的api达到快速开发。
B.cs
public class B
{
public B(){}
private int money = 0;
private string name = string.Empty;
public int Money
{
get { return money; }
set { money = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
}
A.cs:
public class A
{
public A(){}
public int age { get; set; }
public string name { get; set; }
B b = null;
public B B
{
get { return b; }
set { b = value; }
}
}
测试代码如下:
using Newtonsoft.Json;
protected void Page_Load(object sender, EventArgs e)
{
A a = new A();
a.age = 11;
a.name = "Name";
B b = new B();
b.Money = 10000;
//b.Name = "小样";
a.B = b;
string str= JsonConvert.SerializeObject(a);
Response.Write(str);
}
输出:{"age":11,"name":"Name","B":{"Money":10000,"Name":""}}