1

I'm trying to call an API and deserialize the json of output. It gives me the following error when clicking on my view :

JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[MvcSpatial.Models.Weather+Root]' 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 'type', line 2, position 9.*

I'm using IEnumerable in my view but I don't know if this is the cause of the problem.. Any idea?

This is my classes using https://json2csharp.com/

public class weather 
{
    public class Symbol
    {
        [JsonProperty("value")]
        public string Value { get; set; }

        [JsonProperty("type")]
        public string Type { get; set; }
    }

    public class Unit
    {
        [JsonProperty("label")]
        public string Label { get; set; }

        [JsonProperty("symbol")]
        public Symbol Symbol { get; set; }
    }

    public class ObservedProperty
    {
        [JsonProperty("id")]
        public string Id { get; set; }

        [JsonProperty("label")]
        public string Label { get; set; }
    }

    public class MeasurementType
    {
        [JsonProperty("method")]
        public string Method { get; set; }

        [JsonProperty("period")]
        public string Period { get; set; }
    }

    public class DewPoint
    {
        [JsonProperty("type")]
        public string Type { get; set; }

        [JsonProperty("description")]
        public string Description { get; set; }

        [JsonProperty("unit")]
        public Unit Unit { get; set; }

        [JsonProperty("observedProperty")]
        public ObservedProperty ObservedProperty { get; set; }

        [JsonProperty("measurementType")]
        public MeasurementType MeasurementType { get; set; }
    }

    public class Parameters
    {
        [JsonProperty("Dew point")]
        public DewPoint DewPoint { get; set; }
    }

    public class Properties
    {
        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("Dew point")]
        public List<int> DewPoint { get; set; }

        [JsonProperty("time")]
        public List<DateTime> Time { get; set; }
    }

    public class Geometry
    {
        [JsonProperty("type")]
        public string Type { get; set; }

        [JsonProperty("coordinates")]
        public List<double> Coordinates { get; set; }
    }

    public class Feature
    {
        [JsonProperty("id")]
        public string Id { get; set; }

        [JsonProperty("type")]
        public string Type { get; set; }

        [JsonProperty("properties")]
        public Properties Properties { get; set; }

        [JsonProperty("geometry")]
        public Geometry Geometry { get; set; }
    }

    public class Root
    {
        [JsonProperty("type")]
        public string Type { get; set; }

        [JsonProperty("parameters")]
        public Parameters Parameters { get; set; }

        [JsonProperty("bbox")]
        public List<double> Bbox { get; set; }

        [JsonProperty("features")]
        public List<Feature> Features { get; set; }
    }

}

My controller

public async Task<IActionResult> Weather()
{
     List<Root>  weatherlist = new  List<Root>();

     using (var httpClient = new HttpClient())
     {
         using (var response = await httpClient.GetAsync("http:...=GeoJSON"))
         {
                string apiResponse = await response.Content.ReadAsStringAsync();
                 weatherlist = JsonConvert.DeserializeObject<List<Root>>(apiResponse);
        }
    }

    return View(weatherlist);
}

Here the is the json of the API output

{
 "type": "FeatureCollection",
 "parameters": {
 someThings there
 
  }
 },
 "bbox": [
  -0.123,
  0.123,
  0.123,
  0.123
 ],
 "features": [
  {
   "id": "EGWU",
   "type": "Feature",
   "properties": {
    "name": "tt",
    "Dew point": [
     -5
    ],
    "time": [
     "2022-01-06T04:50:00Z"
    ]
   },
   "geometry": {
    "type": "Point",
    "coordinates": [
     -0.417,
     51.55
    ]
   }
  },
  {
   "id": "EGMC",
   "type": "Feature",
   "properties": {
    "name": "SOUTHEND-ON-SEA",
    "Dew point": [
     -2
    ],
    "time": [
     "2022-01-06T04:50:00Z"
    ]
   },
   "geometry": {
    "type": "Point",
    "coordinates": [
     0.7,
     51.567
    ]
   }
  },
  {
   "id": "EGGW",
   "type": "Feature",
   "properties": {
    "name": "LUTON AIRPORT",
    "Dew point": [
     -3,
     -4
    ],
    "time": [
     "2022-01-06T04:50:00Z",
     "2022-01-06T04:20:00Z"
    ]
   },
   "geometry": {
    "type": "Point",
    "coordinates": [
     -0.367,
     51.867
    ]
   }
  },
  {
   "id": "EGSS",
   "type": "Feature",
   "properties": {
    "name": "STANSTED AIRPORT",
    "Dew point": [
     -4,
     -3
    ],
    "time": [
     "2022-01-06T04:50:00Z",
     "2022-01-06T04:20:00Z"
    ]
   },
   "geometry": {
    "type": "Point",
    "coordinates": [
     0.217,
     51.883
    ]
   }
  },
  {
   "id": "EGKK",
   "type": "Feature",
   "properties": {
    "name": "LONDON/GATWICK A",
    "Dew point": [
     -4,
     -5
    ],
    "time": [
     "2022-01-06T04:50:00Z",
     "2022-01-06T04:20:00Z"
    ]
   },
   "geometry": {
    "type": "Point",
    "coordinates": [
     -0.167,
     51.133
    ]
   }
  },
  {
   "id": "EGLF",
   "type": "Feature",
   "properties": {
    "name": "FARNBOROUGH",
    "Dew point": [
     -6,
     -6
    ],
    "time": [
     "2022-01-06T04:50:00Z",
     "2022-01-06T04:20:00Z"
    ]
   },
   "geometry": {
    "type": "Point",
    "coordinates": [
     -0.767,
     51.283
    ]
   }
  }
 ]
}

Update: this is my view

@Model IEnumerable<weather>

<table class="table table-sm table-striped table-bordered m-2">
    <thead>
        <tr>
            <th>type</th>
          
             <th>Bbox</th>
             
        </tr>
    </thead>
    <tbody>
        @foreach (var r in Model)
        {
            <tr>
                <td>@r.type</td>            
               <td>@r.Bbox</td>                            
            </tr>
        }
</tbody>
    
</table>
3
  • you have to show us your view in order to select a right model Commented Jan 7, 2022 at 17:09
  • Thanks , but your view and model are differs, it can be working as it is. Commented Jan 7, 2022 at 17:29
  • I am trying to figure what do you want to post in your view, since you don' t know. Maybe you will have to find out what is needed? Commented Jan 7, 2022 at 17:38

1 Answer 1

1

try this, your json root is an object, not an array so you have to use Root instead of List

 Root featureCollection=null;

string apiResponse = await response.Content.ReadAsStringAsync();
featureCollection = JsonConvert.DeserializeObject<Root>(apiResponse);

List<Feature> features=featureCollection.Feautures;

return View(features);

view

@model List<Feature>

<table class="table table-sm table-striped table-bordered m-2">
    <thead>
        <tr>
            <th>Feature</th>
             <th>Property Name</th>
              <th>Property DewPoint</th>
              </tr>
    </thead>
    <tbody>
    @foreach (var feature in Model)
    {
        @foreach (var property in feature.Properties)
        {
            <tr>
                <td>@feature.Id</td>            
               <td>@property.Name</td> 
               <td>@property.DewPoint[0] : @property.DewPoint[1] </td>                               
            </tr>
        }
    }

</tbody>
    
</table>
Sign up to request clarification or add additional context in comments.

5 Comments

@ChristophLütjen Yes, you are right , json root is an object, not an array
@Serge so what do you mean.? you code is the same as my code?
@FadiAkkad Sorry, I updated my code. And you will have to show your view in order I could post the last line of code. Without this I don't know what should be your view model.
@FadiAkkad But Bbox is the same, it doesnt depend on the feature name. and feature doesn have name do you mean properties name? But each feature has several properties. YOu have to decide what are going to show in your view
@FadiAkkad I updated my answer, try again

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.