Jamie Rees
Jamie Rees

Reputation: 8183

Cannot deserialize JSON to strongly typed object

So this is my JSON string

[
  {
    "title": "Archer (2009)",
    "seasonCount": 5,
    "episodeCount": 47,
    "episodeFileCount": 47,
    "status": "continuing",
    "overview": "At ISIS, an international spy agency, global crises are merely opportunities for its highly trained employees to confuse, undermine, betray and royally screw each other. At the center of it all is suave master spy Sterling Archer, whose less-than-masculine code name is \"Duchess.\" Archer works with his domineering mother Malory, who is also his boss. Drama revolves around Archer's ex-girlfriend, Agent Lana Kane and her new boyfriend, ISIS comptroller Cyril Figgis, as well as Malory's lovesick secretary, Cheryl.",
    "nextAiring": "2014-01-21T03:00:00Z",
    "network": "FX",
    "airTime": "7:00pm",
    "images": [
      {
        "coverType": "banner",
        "url": "/MediaCover/7/banner.jpg?lastWrite=635114443841234311"
      },
      {
        "coverType": "poster",
        "url": "/MediaCover/7/poster.jpg?lastWrite=635114443843262329"
      },
      {
        "coverType": "fanart",
        "url": "/MediaCover/7/fanart.jpg?lastWrite=635114443844822347"
      }
    ],
    "seasons": [
      {
        "seasonNumber": 5,
        "monitored": true
      },
      {
        "seasonNumber": 4,
        "monitored": true
      },
      {
        "seasonNumber": 3,
        "monitored": true
      },
      {
        "seasonNumber": 2,
        "monitored": true
      },
      {
        "seasonNumber": 1,
        "monitored": true
      },
      {
        "seasonNumber": 0,
        "monitored": false
      }
    ],
    "year": 0,
    "path": "T:\\Archer (2009)",
    "qualityProfileId": 1,
    "seasonFolder": true,
    "monitored": true,
    "useSceneNumbering": false,
    "runtime": 30,
    "tvdbId": 110381,
    "tvRageId": 23354,
    "firstAired": "2009-09-18T02:00:00Z",
    "lastInfoSync": "2014-01-17T05:10:23.253208Z",
    "seriesType": "standard",
    "cleanTitle": "archer2009",
    "imdbId": "tt1486217",
    "titleSlug": "archer-2009",
    "id": 1
  }
]

Here is the object I am attempting to deserialize to:

public class SonarrSeriesWrapper
{
    public SonarrSeries[] SonarrSeries { get; set; }
}

public class Image
{
    public string coverType { get; set; }
    public string url { get; set; }
}

public class Season
{
    public int seasonNumber { get; set; }
    public bool monitored { get; set; }
}

[JsonArray]
public class SonarrSeries
{
    public string title { get; set; }
    public int seasonCount { get; set; }
    public int episodeCount { get; set; }
    public int episodeFileCount { get; set; }
    public string status { get; set; }
    public string overview { get; set; }
    public string nextAiring { get; set; }
    public string network { get; set; }
    public string airTime { get; set; }
    public List<Image> images { get; set; }
    public List<Season> seasons { get; set; }
    public int year { get; set; }
    public string path { get; set; }
    public int qualityProfileId { get; set; }
    public bool seasonFolder { get; set; }
    public bool monitored { get; set; }
    public bool useSceneNumbering { get; set; }
    public int runtime { get; set; }
    public int tvdbId { get; set; }
    public int tvRageId { get; set; }
    public string firstAired { get; set; }
    public string lastInfoSync { get; set; }
    public string seriesType { get; set; }
    public string cleanTitle { get; set; }
    public string imdbId { get; set; }
    public string titleSlug { get; set; }
    public int id { get; set; }
}

And my deserializing code:

var s = JsonConvert.DeserializeObject<SonarrSeriesWrapper>(jsonData);
return s;

But I am getting the following exception:

An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll but was not handled in user code

Additional information: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'NZBDash.Common.Models.Api.SonarrSeriesWrapper' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.

Path '', line 1, position 1.

Now I have read that it is because the JSON starts with a [ so it thinks it's an JsonArray, but I thought the SonarrSeriesWrapper would solve that (deseralizing it like this also causes the error JsonConvert.DeserializeObject<List<SonarrSeries>>(jsonData);)

Anyone got any idea on why it's not working?

Error when using

JsonConvert.DeserializeObject<List<SonarrSeries>>(jsonData);

An exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll but was not handled in user code

Additional information: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'NZBDash.Common.Models.Api.SonarrSeries' 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 '[0].title', line 3, position 13.

Upvotes: 2

Views: 678

Answers (1)

Hamid Pourjam
Hamid Pourjam

Reputation: 20754

You should remove JsonArray attribute from SonarrSeries class and deserialize the json like this.

var s = JsonConvert.DeserializeObject<SonarrSeries[]>(jsonData)[0];
return s;

You don't need SonarrSeriesWrapper anymore by using this method.

Upvotes: 3

Related Questions