0

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.

1
  • This is the whole json file. I shared all of it. Commented May 11, 2019 at 19:56

2 Answers 2

3

Assuming that your JSON looks like:

{
   "samples":[ ... ]
}

You have an outer container object that encapsulates your array of samples. You need to deserialize to a data model that reflects this structure. One easy way to do it is to use an anonymous object, along with JsonConvert.DeserializeAnonymousType():

var result = JsonConvert.DeserializeAnonymousType(jsonStr, new { samples = default(List<SampleClass>) })
    .samples;

This approach avoids loading the JSON into an intermediate dynamic or JToken representation.

Sample fiddle here.

Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

dynamic json = Newtonsoft.Json.JsonConvert.DeserializeObject(json_string);
List<SampleClass> result = JsonConvert.DeserializeObject<List<SampleClass>>(json.samples.ToString());

but if it doesn't,use this

 List<SampleClass> result = new List<SampleClass>();
 foreach (var item in json.samples)
 {               
  result.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<SampleClass>(item.ToString()));
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.