locked
Error while deserializing JSON RRS feed

  • Question

  • Dear all,
    I am trying to convert JSON data to List class so I can read the data.
    While trying, I am getting an error, Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1 because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

    I am keeping the code and the details below, please advise me to rectify this issue. Thank you for your support.

    c# code:
    var lst = JsonConvert.DeserializeObject<List<Set>>(jsonDataString)

    The JSON structure of variable, jsonDataString and the class structure of Set object are below.

    JSON Data((jsonDataString):
    {
        "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
            }
           ]
     }
    =============
    class(Set) structure:
    =============
     public class Set
        {
            public List<Sample> Samples { get; set;}
        }

        public class Sample
        {        
            public DateTime dt { get; set; }
            public double temp { get; set; }
            public int ph { get; set; }
            public int phosphate { get; set; }
            public int chloride { get; set; }
            public int nitrate { get; set; }
        }


    Narayana Reddy G

    Wednesday, June 24, 2020 12:42 PM