Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
792 views
in Technique[技术] by (71.8m points)

json - object to deserialize has a C# keyword

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 another way to tell it what the field names will be?

Here's an example of the JSON:

{ event: 123 data: {"data":"0D0401","ttl":"60","published_at":"2014-04-16T18:04:42.446Z","id":"48ff6f065067555031192387"} }

Here are my classes that won't compile because of the keyword:

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

public class EventDetail
{
    public string data { get; set; }
    public string ttl { get; set; }
    public DateTime published_at { get; set; }
    public string id { get; set; }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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. ''', ' ', 'u20AC' (the Euro € currency sign), 'x9'

(equivalent to )) - literal string escaping:

e.g. "......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. iu0064 (for id)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...