I got some Json, that looks like this:
[
{
"starttime": "2020-02-27T14:30:00Z",
"endtime": "2020-02-27T14:40:00Z"
},
{
"Temp": {
"value": 3
},
"Pressure": {
"value": 29
},
"Humidity": {
"value": 85
}
}
]
I would like to deserialize it onto a object on the form:
public class Sample {
public string Name {get; set;}
public int Value {get;set;}
}
and then get 3 instances where name is set to either Temp, Pressure, Humidity, and Value set to 3, 29, 85 I don't really care about the start-/endtime part.
Any help would be greatly appreciated...
/Søren
Update:
Came up with this myself:
var tmp = JsonConvert.DeserializeObject<JArray>(content);
var samples = tmp.
SelectMany(x => ((JToken) x).Children())
.Where(x => !((JProperty) x).Name.Contains("time"))
.Select(x =>
{
var tmp2 = x.First.ToObject<Sample>();
tmp2.name = ((JProperty) x).Name;
return tmp2;
})
.ToList();
but I think Pavel's solution below, is more readable....
Newtonsoft.Jsonlibrary from NuGet - newtonsoft.com/json