Reputation: 1601
I am trying to retrieve JSON data from an API, but one of the property names comes as @data.context
. It is not a nested property. I've tested it with ExpandoObject as well, it is exactly like that. Normally in C # I would make a data model like
public class Data
{
public string @odata.context { get ; set; }
}
But this doesn't work, as C# doesn't let me have a comma in the variable name, nor have quotes around it. The @
sign is already there
The JSON is as follows: this property that contains a link and then another one that contains a list of objects.
{
"@odata.context": "some link here",
"list" [ {}, {}
]
}
The list of objects do not give me any trouble, only the first property.
Upvotes: 1
Views: 400
Reputation: 1934
You can use JsonPropertyName attribute to map the json to the property e.g:
[JsonPropertyName("@odata.context")]
public string DataContext { get ; set; }
Upvotes: 3
Reputation: 1512
You might be consuming a poorly designed API. There are API specifications that tells how to structure and name JSON keys. One way you can accomplish is
1 Fetch JSON response from API
2 Replace "@data.context" with "Context" or something similar in JSON string
3 Create class with property
public class Data
{
public string Context { get ; set; }
}
4 Deserialise it
Upvotes: 2