I have the following problem in my program:
I receive a JSON with the following structure:
then when deserializing the json it goes well, I have the objects and their keys, but the problem is that the keys have the "-" sign, so I can't
var listProduct = JsonConvert.DeserializeObject<List<ExpandoObject>>(jResult);
foreach (dynamic prod in listProduct)
{
int aid = Convert.ToInt32(prod.aid.ToString());
string title = prod.title;
DateTime sentenceDate = Convert.ToDateTime(prod.property-value_546_iso8601);
Suseso mySuseso = new Suseso(.......);
}
accessing through the index is not possible since the same fields do not always come in the same index. and for example there is a key called "abstract" and if I place miJson.abstract, it throws me an error because it is prohibited.
I need to be able to get the value of :
miJson.property-value_546_iso8601;
miJson.abstract;
In short, how do I change the name of a KEY of my deserialized json?
From already thank you very much.
The best way to do it would be through your model, create a class which contains your keys, and assign the Json Propery label to each of the properties to indicate how each of these properties should be assigned in relation to the JSON which you are trying to deserialize:
In this example we see how release_date is the key of your json which when you do the deserialization will be mapped to the ReleaseDate property and you will be able to use it in all your code without having to worry about the famous reserved words
I enclose the NewtonSoft link in case you want to know more about the subject: https://www.newtonsoft.com/json/help/html/JsonPropertyName.htm