Lord Vermillion Lord Vermillion - 1 year ago 63
JSON Question

Deserialize JSON with '@' in attribute name

I have a JSON-object that contains an attribute named @id:

{"@id": "231"}

In my object i tried both:

[DataMember(Name = "@id")]
public string id { get; set; }


public string @id { get; set; }

but when i deserialize with json.net the id always gets null:


How can i deserialize the @id attribute?

Answer Source

try like this -

class Root{
   public string id { get; set; }

Test -

var json = "{'@id': '231'}";
var t = JsonConvert.DeserializeObject<Root>(json);
Console.WriteLine(t.id); //231
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download