2010-07-16 98 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; 
}