2010-07-16 219 views
3

Json.net我們可以重命名[JsonPropertyAttribute("")]財產,重命名JSON序列化屬性

 public class Foo 
     { 
      // how can I rename the Foo1 property name to F1?! 
      public string Foo1 { set; get; } 
      public string Foo2 { set; get; } 

     } 

,並在後面的Web服務代碼:

[WebMethod] 
    public List<Foo> GetFoos() 
    { 
     List<Foo> post = new List<Foo> 
          { 
           new Foo(), 
           new Foo(), 
           new Foo() 
          }; 
     return post; 
    } 

謝謝你,

-ali

回答

4

例如,如果您使用DataContractJsonSerializer(參見http://msdn.microsoft.com/en-us/library/bb412179.aspx)你可以申報以下

[DataContract(Name = "User")] 
struct Person 
{ 
    [DataMember(Name = "FirstName")] 
    public string Name; 

    [DataMember(Name = "LastName")] 
    public string Surname; 
}