Home > database >  C# JSON deserialize: failing stating I need an array when everything is already an array
C# JSON deserialize: failing stating I need an array when everything is already an array

Time:05-19

I must be blind. This is what I am receiving from API:

{
    "temperatures": [
        {
            "field": "temp1",
            "values": [
                {
                    "value": 60.5,
                    "time": "2022-05-19T09:28:01.732662315Z"
                }
            ]
        },
        {
            "field": "temp2",
            "values": [
                {
                    "value": 33.8,
                    "time": "2022-05-19T09:28:01.732662315Z"
                }
            ]
        },
        {
            "field": "temp3",
            "values": [
                {
                    "value": 27.6,
                    "time": "2022-05-19T09:28:01.732662315Z"
                }
            ]
        },
        {
            "field": "temp4",
            "values": [
                {
                    "value": 26.6,
                    "time": "2022-05-19T09:28:01.732662315Z"
                }
            ]
        }
    ]
}

With this being my object model:

public class Temperatures
    {
        public temperatures[] temperatures { get; set; }
    }

    public class temperatures
    {
        public string field { get; set; }
        public TempValues[] values { get; set; }
    }

    public class TempValues
    {
        public string value { get; set; }
        public string time { get; set; }
    }

But these lines fail:

var response = await client.GetAsync(url);
            string content = (response.Content.ReadAsStringAsync().Result);
 ObservableCollection<Temperatures> res = JsonConvert.DeserializeObject<ObservableCollection<Temperatures>>(content);

saying that:

{Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.ObjectModel.ObservableCollection`1[oktopus.Helpers.Types.Temperatures]' because the type requires a JSON array (e.g. [1…}

What am I missing here? I tried making the observarble collection be an array, but this fails with the same error. Also I use the observable collection everywhere else with this API and this works, so it must be an issue with my object model. Can you spot it?

CodePudding user response:

You're trying to deserialize to a collection of Temperatures, which would only be valid if the JSON started with [ and ended with ]. Instead, your JSON represents a single Temperatures object, which then contains an array as the value of the temperatures property.

So you just want:

Temperatures res = JsonConvert.DeserializeObject<Temperatures>(content);

(There are various other things you may well want to change, such as using conventional .NET names in the code and specifying attributes to indicate how they should be represented in JSON, and using a numeric type in TempValue, but the above should solve the immediate problem.)

CodePudding user response:

JSON does not contain array, it contains single object with array temperatures, that's why serialzer cannot deserialize it as a collection. Do it that way:

var temperatures = JsonConvert.DeserializeObject<Temperatures>(content);
ObservableCollection<Temperatures> res = new ObservableCollection<Temperatures>(temperatures.temperatures);
  • Related