object to deserialize has a C# keyword

后端 未结 3 1725
轻奢々
轻奢々 2020-12-07 00:04

With the JSON defined as it is, in order to deserialize it as an object, I\'d need to create a property on my class called \"event\", which is a C# keyword. Is there anothe

相关标签:
3条回答
  • 2020-12-07 00:39

    Try using the [DataContract(Name = "@event")] attribute on the relevant property. Then it will (de)serialize correctly, and you can rename the property so that it compiles.

    0 讨论(0)
  • 2020-12-07 00:46

    Change

    public class Event
    {
        public int event { get; set; }
        public EventDetail data { get; set; }
    }
    

    to this

    public class Event
    {
        public int @event { get; set; }
        public EventDetail data { get; set; }
    }
    

    This tip shows the quirks involved with escaping in C#:

    • character literal escaping:

    e.g. '\'', '\n', '\u20AC' (the Euro € currency sign), '\x9'

    (equivalent to \t)) - literal string escaping:

    e.g. "...\t...\u0040...\U000000041...\x9..."

    • verbatim string escaping:

    e.g. @"...""..."

    • string.Format escaping:

    e.g. "...{{...}}..."

    • keyword escaping:

    e.g. @if (for if as identifier)

    • identifier escaping:

    e.g. i\u0064 (for id)

    0 讨论(0)
  • 2020-12-07 00:52

    I was able to just capitalize the "e", and it still works. Looks like the parsing mechanism is case-insensitive.

    0 讨论(0)
提交回复
热议问题