Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructing C# object for the JSON [duplicate]

I am trying to create a C# objects for the JSON response in my application. I have the JSON like below

 {
"@odata.context": "https://example.com/odata/$metadata#REQ",
"value": [
    {
        "Id": 17,
        "Name": "Req"
    }
  ]
 }

I am not sure how to create C# object for the @odata.context

public class RootObject
    {
        public string @odata.context { get; set; }
        public List<Value> value { get; set; }
    }

It throws error in @odata.context

like image 465
user4912134 Avatar asked Oct 19 '17 12:10

user4912134


1 Answers

That's because identifiers in C# can't have @ sign. You haven't stated what library you're using, but if it's JSON.NET then you can simply decorate the properties.

public class Root
{
    [JsonProperty("@odata.context")]
    public string OdataContext { get; set; }

    [JsonProperty("value")]
    public List<Value> Value { get; set; }
}

public class Value
{
    [JsonProperty("Id")]
    public long Id { get; set; }

    [JsonProperty("Name")]
    public string Name { get; set; }
}
like image 167
Equalsk Avatar answered Nov 19 '22 06:11

Equalsk