0

I need to receive a data and convert it to Json changing the name of a property:

public class MyClass {
     public string X { get; set;}
}

public IHttpActionResult Method(MyClass model)
{
    //here my property 'X' needs to be converted into 'Y'
    var json = JsonConvert.SerializeObject(model);

    return OK(json);
}

I tried to use JsonProperty like this:

public class MyClass {
     [JsonProperty(PropertyName = "Y")]
     public string X { get; set;}
}

But this does not work because when binding the property on action, it looks for a 'Y' and not for my 'X', so the property 'X' comes null;

So how can I just ignore model binding and converts my 'X' to 'Y' or how can I change the property name when serializing?.

MuriloKunze
  • 15,195
  • 17
  • 54
  • 82
  • Can you show us your binding? Renaming should be possible: http://stackoverflow.com/questions/8796618/how-can-i-change-property-names-when-serializing-with-json-net But I have no clue what you mean with the rest. – RvdK Jan 24 '17 at 14:36
  • Give it a try http://stackoverflow.com/questions/26882986/overwrite-json-property-name-in-c-sharp – Ali Baig Jan 24 '17 at 14:36

0 Answers0