Json.net的常用语句JsonConvert.SerializeObject(对象)

在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":""}}

转载请注明:代码家园 » Json.net的常用语句JsonConvert.SerializeObject(对象)

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)