i'm trying to deserialize the following json:
{
"oxide":{
"Al2O3":"0.3",
"CaO":"0.3",
"FeO":"0.3",
"MgO":"0.3",
"MnO":"0.3",
"SiO2":"0.3"
},
"temperature": "1800"
}
When I convert in this way everthing works:
Oxides oxides = new Oxides();
string oxidequery = req.Query["oxide"];
string temperature = req.Query["temperature"];
string requestBody = new StreamReader(req.Body).ReadToEnd();
dynamic data = JsonConvert.DeserializeObject(requestBody);
oxide.Al2O3 = data?.oxide.Al2O3;
oxide.CaO = data?.oxide.CaO;
oxide.FeO = data?.oxide.FeO;
oxide.MgO = data?.oxide.MgO;
oxide.MnO = data?.oxide.MnO;
oxide.SiO2 = data?.oxide.SiO2;
double tempDouble = temperature ?? data?.temperature;
But when I doing this it not working:
Oxides oxides = new Oxides();
string oxidequery = req.Query["oxide"];
string temperature = req.Query["temperature"];
string requestBody = new StreamReader(req.Body).ReadToEnd();
dynamic data = JsonConvert.DeserializeObject(requestBody);
oxide = (Oxides)data?.oxide;
double tempDouble = temperature ?? data?.temperature;
In the second aproach i get the error
Cannot convert type 'Newtonsoft.Json.Linq.JObject' to 'Oxides'.
What I'm missing? I already searched and found that I have to explicit convert data.oxide, but I'm already doing this.