Rename property in json serialization

in Json.net we can rename the property with [JsonPropertyAttribute("")],

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

        }

and in the web service code behind:

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

Thank you,

-ali

+3
source share
1 answer

For example, if you use DataContractJsonSerializer(see http://msdn.microsoft.com/en-us/library/bb412179.aspx ), you can declare the following

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

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

Source: https://habr.com/ru/post/1755063/


All Articles