user1017882
user1017882

Reputation:

Changing property names for serializing

My class has a property 'PropertyA', I want this to appear as 'PropertyB' in a JSON object when it's serialized. Is there any sort of attribute I can use?

Upvotes: 18

Views: 23488

Answers (1)

Bartosz
Bartosz

Reputation: 3358

For Json.NET and DataContractJsonSerializer use DataMemberAttribute:

[DataMember(Name="PropertyB")]
T PropertyA { ... }

Make sure that your class is decorated with the [DataContract] attribute as well.

If you're using JavaScriptSerializer, you need to create derived implementation, as described here: JavaScriptSerializer.Deserialize - how to change field names

Upvotes: 32

Related Questions