Home > OS >  How to fix error: Cannot deserialize the current JSON object
How to fix error: Cannot deserialize the current JSON object

Time:03-22

I tried to fetch data from API but I receive an error:

{Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[pizhevsoft.Models.ItemsAPI AladinModel]' 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) 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 'aladinModel', line 1, position 15. at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) [0x003a0] in <7ca8898b690a4181a32a9cf767cedb1e>:0 at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) [0x0006d] in <7ca8898b690a4181a32a9cf767cedb1e>:0 at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Boolean checkAdditionalContent) [0x000db] in <7ca8898b690a4181a32a9cf767cedb1e>:0 at Newtonsoft.Json.JsonSerializer.DeserializeInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00054] in <7ca8898b690a4181a32a9cf767cedb1e>:0 at Newtonsoft.Json.JsonSerializer.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType) [0x00000] in <7ca8898b690a4181a32a9cf767cedb1e>:0 at Newtonsoft.Json.JsonConvert.DeserializeObject (System.String value, System.Type type, Newtonsoft.Json.JsonSerializerSettings settings) [0x0002d] in <7ca8898b690a4181a32a9cf767cedb1e>:0 at Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value, Newtonsoft.Json.JsonSerializerSettings settings) [0x00000] in <7ca8898b690a4181a32a9cf767cedb1e>:0 at Newtonsoft.Json.JsonConvert.DeserializeObject[T] (System.String value) [0x00000] in <7ca8898b690a4181a32a9cf767cedb1e>:0 at pizhevsoft.Services.RestServiceAPI.GetAladinData (System.String query) [0x00151] in C:\Users\Admin\Desktop\pizhevsoft\pizhevsoft\pizhevsoft\pizhevsoft\Services\RestServiceAPI.cs:30 }

My object class looks like:

public partial class RootAladinModel
{
    [JsonProperty("aladinModel")]
    public AladinModel[] AladinModel { get; set; }
}

public partial class AladinModel
{
    [JsonProperty("DATS")]
    public DateTimeOffset Dats { get; set; }

    [JsonProperty("TA")]
    public double Ta { get; set; }

    [JsonProperty("RH")]
    public double Rh { get; set; }

    [JsonProperty("WS")]
    public double Ws { get; set; }

    [JsonProperty("RR")]
    public double Rr { get; set; }

    [JsonProperty("SR")]
    public double Sr { get; set; }

    [JsonProperty("APRES")]
    public double Apres { get; set; }
}

My Rest Service Class look like:

public async Task<List<AladinModel>> GetAladinData(string query)
{
    List<AladinModel> weatherData = new List<AladinModel>();
    try
    {
        var response = await _client.GetAsync(query);
        if (response.IsSuccessStatusCode)
        {
            var content = await response.Content.ReadAsStringAsync();
            weatherData = JsonConvert.DeserializeObject<List<AladinModel>>(content);
        }
    }
    catch (Exception ex)
    {
        Debug.WriteLine("\t\tERROR {0}", ex.Message);
    }

    return weatherData;
}

I receive this error in the catch statement.

This is the JSON data:

{"aladinModel":[[{"DATS":"2022-03-16T00:00:00.000Z","TA":1,"RH":56.9,"WS":1.5,"RR":0,"SR":0,"APRES":97772.6},{"DATS":"2022-03-16T03:00:00.000Z","TA":0.7,"RH":58.6,"WS":1,"RR":0,"SR":0,"APRES":97581.8},{"DATS":"2022-03-16T06:00:00.000Z","TA":2.7,"RH":66.1,"WS":1.7,"RR":0,"SR":0,"APRES":97512.6},{"DATS":"2022-03-16T09:00:00.000Z","TA":7.4,"RH":71.5,"WS":2.4,"RR":0,"SR":0,"APRES":97237.5},{"DATS":"2022-03-16T12:00:00.000Z","TA":8.4,"RH":76,"WS":1.7,"RR":0,"SR":0,"APRES":97146},{"DATS":"2022-03-16T15:00:00.000Z","TA":7.1,"RH":85.8,"WS":1.5,"RR":0.1,"SR":0,"APRES":97032.2},{"DATS":"2022-03-16T18:00:00.000Z","TA":3.9,"RH":91.9,"WS":0.7,"RR":0,"SR":0,"APRES":97047.9},{"DATS":"2022-03-16T21:00:00.000Z","TA":4.2,"RH":86.7,"WS":2.1,"RR":0,"SR":0,"APRES":97117.8},{"DATS":"2022-03-17T00:00:00.000Z","TA":4.9,"RH":90.5,"WS":3.1,"RR":0.3,"SR":0,"APRES":97175.7},{"DATS":"2022-03-17T03:00:00.000Z","TA":4.4,"RH":94.3,"WS":1.6,"RR":0.9,"SR":0,"APRES":97240.9},{"DATS":"2022-03-17T06:00:00.000Z","TA":2.8,"RH":92.9,"WS":1,"RR":1.6,"SR":0,"APRES":97632.1},{"DATS":"2022-03-17T09:00:00.000Z","TA":6.2,"RH":65.1,"WS":3.6,"RR":0,"SR":0,"APRES":97670.4},{"DATS":"2022-03-17T12:00:00.000Z","TA":7.1,"RH":64,"WS":3.9,"RR":0,"SR":0,"APRES":97684.8},{"DATS":"2022-03-17T15:00:00.000Z","TA":5.2,"RH":82,"WS":4.5,"RR":0.1,"SR":0,"APRES":97723.2},{"DATS":"2022-03-17T18:00:00.000Z","TA":1.8,"RH":82,"WS":3.3,"RR":0,"SR":0,"APRES":97906.4},{"DATS":"2022-03-17T21:00:00.000Z","TA":0.7,"RH":82.6,"WS":2.6,"RR":0.1,"SR":0,"APRES":98167.4},{"DATS":"2022-03-18T00:00:00.000Z","TA":-0.3,"RH":79.7,"WS":1.6,"RR":0.2,"SR":0.1,"APRES":98345.8},{"DATS":"2022-03-18T03:00:00.000Z","TA":-1.2,"RH":87,"WS":1.8,"RR":0.2,"SR":0.1,"APRES":98387.1},{"DATS":"2022-03-18T06:00:00.000Z","TA":-2,"RH":66.6,"WS":2,"RR":0,"SR":0,"APRES":98589.5},{"DATS":"2022-03-18T09:00:00.000Z","TA":2.1,"RH":43.4,"WS":4.4,"RR":0,"SR":0,"APRES":98570.8},{"DATS":"2022-03-18T12:00:00.000Z","TA":4.2,"RH":37.7,"WS":4,"RR":0,"SR":0,"APRES":98496.9},{"DATS":"2022-03-18T15:00:00.000Z","TA":3.1,"RH":38.2,"WS":3.3,"RR":0,"SR":0,"APRES":98481.8},{"DATS":"2022-03-18T18:00:00.000Z","TA":-0.5,"RH":49.8,"WS":2.8,"RR":0,"SR":0,"APRES":98632.9},{"DATS":"2022-03-18T21:00:00.000Z","TA":-1.6,"RH":46.4,"WS":2.6,"RR":0,"SR":0,"APRES":98658.8},{"DATS":"2022-03-19T00:00:00.000Z","TA":-2.2,"RH":53.2,"WS":1.7,"RR":0,"SR":0,"APRES":98663},{"DATS":"2022-03-19T03:00:00.000Z","TA":-3.4,"RH":64.3,"WS":0.5,"RR":0,"SR":0,"APRES":98617.2},{"DATS":"2022-03-19T06:00:00.000Z","TA":-2.4,"RH":64,"WS":1.5,"RR":0,"SR":0,"APRES":98723.9},{"DATS":"2022-03-19T09:00:00.000Z","TA":1.7,"RH":50.1,"WS":3.3,"RR":0,"SR":0,"APRES":98627.5},{"DATS":"2022-03-19T12:00:00.000Z","TA":3,"RH":45.2,"WS":3.2,"RR":0,"SR":0,"APRES":98513.8},{"DATS":"2022-03-19T15:00:00.000Z","TA":1.7,"RH":53,"WS":3.2,"RR":0.1,"SR":0,"APRES":98504.2},{"DATS":"2022-03-19T18:00:00.000Z","TA":-0.9,"RH":50.1,"WS":3,"RR":0,"SR":0,"APRES":98630},{"DATS":"2022-03-19T21:00:00.000Z","TA":-2.8,"RH":54.3,"WS":1.6,"RR":0,"SR":0,"APRES":98747.2},{"DATS":"2022-03-20T00:00:00.000Z","TA":-3.5,"RH":54.1,"WS":1.5,"RR":0,"SR":0,"APRES":98752.8},{"DATS":"2022-03-20T03:00:00.000Z","TA":-4.5,"RH":59.8,"WS":1.4,"RR":0,"SR":0,"APRES":98672.5},{"DATS":"2022-03-20T06:00:00.000Z","TA":-2.6,"RH":57.7,"WS":1.9,"RR":0,"SR":0,"APRES":98805.5},{"DATS":"2022-03-20T09:00:00.000Z","TA":2.1,"RH":40.3,"WS":2.1,"RR":0,"SR":0,"APRES":98701.4},{"DATS":"2022-03-20T12:00:00.000Z","TA":4.4,"RH":37.6,"WS":1.9,"RR":0,"SR":0,"APRES":98568},{"DATS":"2022-03-20T15:00:00.000Z","TA":3.9,"RH":40.3,"WS":1,"RR":0,"SR":0,"APRES":98467.1},{"DATS":"2022-03-20T18:00:00.000Z","TA":0.2,"RH":50.9,"WS":4.3,"RR":0,"SR":0,"APRES":98518.3},{"DATS":"2022-03-20T21:00:00.000Z","TA":-0.3,"RH":47.7,"WS":2.6,"RR":0,"SR":0,"APRES":98505.9},{"DATS":"2022-03-21T00:00:00.000Z","TA":-2.3,"RH":53.8,"WS":1.6,"RR":0,"SR":0,"APRES":98413.8},{"DATS":"2022-03-21T03:00:00.000Z","TA":-2.4,"RH":54.6,"WS":1.7,"RR":0,"SR":0,"APRES":98278.5},{"DATS":"2022-03-21T06:00:00.000Z","TA":-0.8,"RH":51.8,"WS":1.2,"RR":0,"SR":0,"APRES":98361.4},{"DATS":"2022-03-21T09:00:00.000Z","TA":4.5,"RH":38.3,"WS":2.1,"RR":0,"SR":0,"APRES":98296.6},{"DATS":"2022-03-21T12:00:00.000Z","TA":5.9,"RH":40.8,"WS":2,"RR":0,"SR":0,"APRES":98131.2},{"DATS":"2022-03-21T15:00:00.000Z","TA":5.8,"RH":45.8,"WS":2.1,"RR":0,"SR":0,"APRES":98015.5},{"DATS":"2022-03-21T18:00:00.000Z","TA":1.4,"RH":52.7,"WS":3.8,"RR":0,"SR":0,"APRES":98064.1},{"DATS":"2022-03-21T21:00:00.000Z","TA":0.2,"RH":53.3,"WS":1.9,"RR":0,"SR":0,"APRES":98108.2},{"DATS":"2022-03-22T00:00:00.000Z","TA":-0.8,"RH":60,"WS":3,"RR":0,"SR":0,"APRES":98065},{"DATS":"2022-03-22T03:00:00.000Z","TA":0.5,"RH":52.9,"WS":3,"RR":0,"SR":0,"APRES":97926.2},{"DATS":"2022-03-22T06:00:00.000Z","TA":3.7,"RH":50.3,"WS":4.4,"RR":0,"SR":0,"APRES":97894.1},{"DATS":"2022-03-22T09:00:00.000Z","TA":8.2,"RH":50.8,"WS":3.4,"RR":0,"SR":0,"APRES":97786},{"DATS":"2022-03-22T12:00:00.000Z","TA":11,"RH":51.4,"WS":3,"RR":0,"SR":0,"APRES":97563.2},{"DATS":"2022-03-22T15:00:00.000Z","TA":9,"RH":57.7,"WS":5,"RR":0.5,"SR":0,"APRES":97473.7},{"DATS":"2022-03-22T18:00:00.000Z","TA":5.7,"RH":57.6,"WS":2.9,"RR":0,"SR":0,"APRES":97661.6},{"DATS":"2022-03-22T21:00:00.000Z","TA":2.6,"RH":63.9,"WS":2.5,"RR":0,"SR":0,"APRES":97793.2},{"DATS":"2022-03-23T00:00:00.000Z","TA":0.5,"RH":74.8,"WS":1.3,"RR":0,"SR":0,"APRES":97811.7},{"DATS":"2022-03-23T03:00:00.000Z","TA":0.2,"RH":69.7,"WS":2.1,"RR":0,"SR":0,"APRES":97797.9},{"DATS":"2022-03-23T06:00:00.000Z","TA":2.2,"RH":69.5,"WS":1.1,"RR":0,"SR":0,"APRES":97815.5},{"DATS":"2022-03-23T09:00:00.000Z","TA":6.9,"RH":49.9,"WS":2.2,"RR":0,"SR":0,"APRES":97818.5},{"DATS":"2022-03-23T12:00:00.000Z","TA":9,"RH":43.6,"WS":1.4,"RR":0,"SR":0,"APRES":97653.9},{"DATS":"2022-03-23T15:00:00.000Z","TA":9,"RH":47.1,"WS":1.1,"RR":0,"SR":0,"APRES":97537.8},{"DATS":"2022-03-23T18:00:00.000Z","TA":3.2,"RH":60.4,"WS":1.2,"RR":0,"SR":0,"APRES":97628.5},{"DATS":"2022-03-23T21:00:00.000Z","TA":3.2,"RH":61.6,"WS":3.3,"RR":0,"SR":0,"APRES":97612.5},{"DATS":"2022-03-24T00:00:00.000Z","TA":3.9,"RH":59.4,"WS":3.6,"RR":0,"SR":0,"APRES":97508.8},{"DATS":"2022-03-24T03:00:00.000Z","TA":4.7,"RH":57.9,"WS":4.1,"RR":0,"SR":0,"APRES":97392.8},{"DATS":"2022-03-24T06:00:00.000Z","TA":7,"RH":58.3,"WS":3.8,"RR":0,"SR":0,"APRES":97404.7},{"DATS":"2022-03-24T09:00:00.000Z","TA":12,"RH":50.9,"WS":3.4,"RR":0,"SR":0,"APRES":97354.4},{"DATS":"2022-03-24T12:00:00.000Z","TA":14.5,"RH":42.2,"WS":3.5,"RR":0,"SR":0,"APRES":97181.6},{"DATS":"2022-03-24T15:00:00.000Z","TA":14.2,"RH":45.7,"WS":3.4,"RR":0,"SR":0,"APRES":97068.2},{"DATS":"2022-03-24T18:00:00.000Z","TA":8.4,"RH":56.2,"WS":2.1,"RR":0,"SR":0,"APRES":97086.9},{"DATS":"2022-03-24T21:00:00.000Z","TA":7.7,"RH":52.9,"WS":3.5,"RR":0,"SR":0,"APRES":97200.7}],{"fieldCount":0,"affectedRows":504,"insertId":0,"serverStatus":34,"warningCount":0,"message":"","protocol41":true,"changedRows":0}]}

How can I fix this error?

CodePudding user response:

A very strange yet valid JSON.

enter image description here

You need to get the first item (array) from aladinModel array.

using Newtonsoft.Json.Linq;

var content = await response.Content.ReadAsStringAsync();

JObject root = JObject.Parse(content);
weatherData = root["aladinModel"][0].ToObject<List<AladinModel>>();

Sample program

CodePudding user response:

The JSON that is returned from the API point does not represent a List<AladinModel> but instead represents a RootAladinModel.

Try the following (or similar):

var model = JsonConvert.DeserializeObject<RootAladinModel>(content);
weatherData = model.AladinModel.ToList();
  • Related