C#, Newtonsoft.Json: Cannot deserialize the current JSON array

moondaddy 916 Reputation points
2021-12-14T20:33:32.353+00:00

Using c# 4.8, I need to convert the following json into a list of objects but get the following exception:

Cannot deserialize the current JSON array ... because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

Even though it parses in a json parser.

[
  {
    "Isu_Id": "0FC5FA46-34A7-4B69-B63A-1DEFD8951D28",
    "Isu_404Url": "http:\\/\\/cmtafr-dev.crocodiledigital.net\\/IssueImagesOriginal\\/0FC5FA46-34A7-4B69-B63A-1DEFD8951D28.jpg"
  },
  {
    "Isu_Id": "DB396D6E-AF70-43D8-B03D-2314AD1F8C9C",
    "Isu_404Url": "http:\\/\\/cmtafr-dev.crocodiledigital.net\\/IssueImagesOriginal\\/DB396D6E-AF70-43D8-B03D-2314AD1F8C9C.jpg"
  }
]

I thought it might be the escape charectors so tried using the following text but get the same error:

[
  {
    "Isu_Id": "0FC5FA46-34A7-4B69-B63A-1DEFD8951D28",
    "Isu_404Url": "http"
  },
  {
    "Isu_Id": "DB396D6E-AF70-43D8-B03D-2314AD1F8C9C",
    "Isu_404Url": "http"
  }
]

and this is my c# code:

public class Issue_Image404ModelList
{
    public List<Issue_Image404Model> ListOf404Items { get; set; }
}

and the exception occurs on this line:

var items = JsonConvert.DeserializeObject<Issue_Image404ModelList>(requestContent);

Can someone please kindly tell me how to resolve this?

Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. moondaddy 916 Reputation points
    2021-12-14T20:56:34.003+00:00

    I found the answer. The problem was the type of class I was trying to convert the json to. I was missing the " : List<type>"

    This worked:

    public class Issue_Image404ModelList: List<Issue_Image404Model>
    {
        public List<Issue_Image404Model> ListOf404Items { get; set; }
    }
    

    and then this:

    Issue_Image404ModelList items = JsonConvert.DeserializeObject<Issue_Image404ModelList>(requestContent);
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.