Lord Vermillion Lord Vermillion - 23 days ago 8
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; }


and

public string @id { get; set; }


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

JsonConvert.DeserializeObject<TestObc>(jsonString);


How can i deserialize the @id attribute?

Answer

try like this -

class Root{
   [JsonProperty("@id")]
   public string id { get; set; }
}

Test -

var json = "{'@id': '231'}";
var t = JsonConvert.DeserializeObject<Root>(json);
Console.WriteLine(t.id); //231