5

do we have something like what Golang has the json annotation?

type FieldType struct {
    TypeName      string          `json:"typeName"`
    CodeType      string          `json:"codeType"`
    Suffix        string          `json:"suffix"`
    PropertiesRaw json.RawMessage `json:"properties"`
    Properties    FieldTypePropertyMap
}

I have a json string:

{ "long_name":"dffdf" }

My class:

public class Result
{
  public int LongName {get; set;}
}

Because of the underscore, the LongName is always null and I do not want to use underscore in my class property.

Is there an option to ignore the underscore when deserializing?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Franva
  • 6,565
  • 23
  • 79
  • 144
  • 1
    Looks like a duplicate of [C# JSON.NET convention that follows Ruby property naming conventions?](https://stackoverflow.com/a/3938546) and [Use different name for serializing and deserializing with Json.Net](https://stackoverflow.com/a/44633105) and [Automatically bind pascal case c# model from snake case JSON in WebApi](https://stackoverflow.com/q/54528475): you need to use `SnakeCaseNamingStrategy`. – dbc Apr 15 '19 at 17:59
  • 1
    Or you could do it on a per-property basis as shown in [.NET NewtonSoft JSON deserialize map to a different property name](https://stackoverflow.com/q/15915503). – dbc Apr 15 '19 at 18:01
  • 1
    thanks @dbc yep, I was only searching for something like :json ignore underscore. That's why I didn't find these questions. Thanks for your help :) – Franva Apr 15 '19 at 23:57

1 Answers1

8

Not so much ignore but you can decorate with a property name like so:

public class Result
{
    [JsonProperty(PropertyName = "long_name")]
    public int LongName { get; set; }
}
Travis Acton
  • 4,292
  • 2
  • 18
  • 30
  • thanks Travis, I am sure this will work, I ended up with the JsonSerializerSettings as it is more convenient. For fine tune, will use this way. – Franva Apr 15 '19 at 23:56