I can parse up to one nested level but I'm unable to figure out how to read the data in the consumption_history
. I have been trying different ways to do it but could not get the consumption value.
I can access the miu_id
and meter_number
but not the array of consumption_list
.
Model:
class JsonModel
{
public class Rootobject
{
public string site_id { get; set; }
public Endpoint[] endpoints { get; set; }
public Paging paging { get; set; }
}
public class Paging
{
public int page { get; set; }
public int limit { get; set; }
public int total { get; set; }
public string next { get; set; }
public object prev { get; set; }
public string self { get; set; }
}
public class Endpoint
{
public string miu_id { get; set; }
public string meter_number { get; set; }
public Consumption_History[] consumption_hist { get; set; }
}
public class Consumption_History
{
public DateTime reading_date { get; set; }
public float consumption { get; set; }
public float consumption_with_multiplier { get; set; }
}
}
Program:
static void Main(string[] args)
{
string json = File.ReadAllText(@"C:\Users\ConsoleApp3\apidataone.json");
var results = JsonConvert.DeserializeObject<JsonModel.Rootobject>(json);
JsonModel.Rootobject rootobject = JsonConvert.DeserializeObject<JsonModel.Rootobject>(json);
rootobject.endpoints = JsonConvert.DeserializeObject<JsonModel.Rootobject>(json).endpoints;
foreach (JsonModel.Consumption_History ch in rootobject.endpoints)
{
Console.WriteLine(ch.consumption);
}
}
json data:
{
"site_id":"1",
"endpoints":
[{
"miu_id":"111",
"meter_number":"88",
"consumption_history":
[{
"reading_date":"2010-02-17T00:00:00",
"consumption":1.0,
"consumption_with_multiplier":1.0
}]
}]
}