Using the method JsonConvert.DeserializeObject returns the default values for all properties.
var current = JsonConvert.DeserializeObject<Current>(myJson);
{
"location": {
"name": "London"
},
"current": {
"temp_c": 5.0,
"cloud": 50
}
}
public class Current
{
public double Temp_c { get; set; }
public double Cloud { get; set; }
}
The expected current object should have the values: 50 for Cloud
, and 5.0 for Temp_c
, but returns the default values for all properties.
2
Answers
You need to define a class model like json object
and then deserialize to it
and then
and get the current value from data object
your ‘Current’ class is far of been like the JSON you post.
You need to convert the JSON string to a C# class. You can use QuickType to convert it (Newtonsoft compatible).
Note: I am using System.Text.Json.Serialization but the class model should be equal, just change:
to
(replace "temp_c" for every name)
Here is the class model (a complete console application) you need:
Now the Deserializer will work OK.