2

Here's my issue : I have an object that will be serialized and send to SendGrid.

public class CustomFields
{
    [JsonProperty("e1_T")]
    public string? AdherentType { get; set; }     
}

As you can see, I need the property name to be "e1_T" in the Json. This way works fine but I need now to externalize the name of the JsonProperty to put it in a conf file that change betwen environement. I tried things like

public class CustomFields
{
    public string fieldName = "wololo";
    
    [JsonProperty(fieldName)]
    public string? AdherentType { get; set; }     
} 

But it won't do the trick.

Any Idea how to make this append ?

D.Rosado
  • 5,634
  • 3
  • 36
  • 56
Gwen
  • 21
  • 2
  • 4
    I think the principles of what you're looking for can be found in this question:https://stackoverflow.com/questions/37917164/newtonsoft-json-dynamic-property-name – sr28 Jun 28 '21 at 11:08
  • 2
    Consider changing your data structure - dictionary of String to String (instead of class with String fields) would be more suitable for this job – Yair Maron Jun 28 '21 at 11:14
  • There is a section about custom serializer/converter on newtonsof samples and I would write one of those. That way you can get the name and put it where you need it like config file. – AliK Jun 28 '21 at 11:37

0 Answers0