I have a JSON file such as:
{
"samples":[
{
"date":"2014-08-10T09:00:00Z",
"temperature":10,
"pH":4,
"phosphate":4,
"chloride":4,
"nitrate":10
},
{
"date":"2014-08-12T09:05:00Z",
"temperature":10.5,
"pH":5,
"chloride":4,
"phosphate":4
},
{
"date":"2014-08-14T09:02:00Z",
"temperature":10.8,
"pH":3,
"phosphate":4
},
{
"date":"2014-08-16T09:02:00Z",
"temperature":11.2,
"pH":6,
"chloride":4
},
{
"date":"2014-08-18T08:58:00Z",
"temperature":10.9,
"pH":10,
"chloride":4,
"phosphate":4,
"nitrate":10
},
{
"date":"2014-08-20T09:10:00Z",
"temperature":9.3,
"pH":6,
"phosphate":4
},
{
"date":"2014-08-22T09:01:00Z",
"temperature":9.5,
"pH":10,
"chloride":4,
"phosphate":4,
"nitrate":10
}
]
}
I need to access each sub-element like "temperature", "pH" and so on to calculate their avarage. To do this, I wanted to use deserializer. First of all, I created a custom class to hold json's parameters,
public class SampleClass
{
public float temperature { get; set; } = 0;
public int pH { get; set; } = 0;
public int phosphate { get; set; } = 0;
public int chloride { get; set; } = 0;
public int nitrate { get; set; } = 0;
}
Right after, I applied the deserializer with using this custom class;
List<SampleClass> json = JsonConvert.DeserializeObject<List<SampleClass>>(jsonStr);
With the above line of code, I am taking an error;
Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[GeneralKnowledge.Test.App.Tests.SampleClass]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'samples', line 2, position 14.
What would you suggest me to solve it? Thanks in advance.