2

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; }
}
tele-bird
  • 183
  • 1
  • 11

4 Answers4

5

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)

123 456 789 0
  • 10,565
  • 4
  • 43
  • 72
1

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.

Martin Costello
  • 9,672
  • 5
  • 60
  • 72
  • But then I'd have to include the `System.Runtime.Serialization` namespace, which wouldn't be needed otherwise. Are the `DataContract` and `DataMember` attributes supported by `Newtonsoft.Json`? – tele-bird Apr 16 '14 at 19:33
  • I believe so. I've used them myself in code for calling REST APIs, and I believe that the HTTP Client libraries use `Newtonsoft.Json` under the covers. – Martin Costello Apr 16 '14 at 19:35
  • Hey, that's cool. It works like you said. Not sure what you mean about HTTP... I'm just using `Event obj = JsonConvert.DeserializeObject(string)` to deserialize it. – tele-bird Apr 16 '14 at 19:42
  • Never mind about HTTP Client, as long as it works :) – Martin Costello Apr 16 '14 at 19:44
  • Even better, I find that I can use the JsonProperty attribute to define the field name, as in this post: [link]http://stackoverflow.com/questions/4671044/deserializing-json-responses-which-contain-attributes-that-conflict-with-keyword. And I don't need to #include the Serialization namespace. – tele-bird Apr 16 '14 at 19:48
1

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

tele-bird
  • 183
  • 1
  • 11
-1

Just in any case someone revisits this... in DotNet 3.1 using System.Text.Json.Serialization use the attribute

[JsonPropertyName("TheNameItHasInJsonFile")]
public int Something {get;set;}
Ramil Aliyev 007
  • 4,437
  • 2
  • 31
  • 47