-1

I'm receiving the following error when executing my code:

JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'AzureWinWorkloadList.AzureWinWorkloadList+Data[]' 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 'SkipToken', line 2, position 14.

This is a standalone console app not part of a MVC type project. Also, this had previously worked for me a few weeks back but when I went back to it, now I get that error.

Here is what my JSON looks like:

{
  "SkipToken": null,
  "Data": [
    {
      "name": "5678-PLACE-32",
      "OSType": "Windows",
      "CompName": "COMPUTER001",
      "RGName": "RG1234",
      "SubID": "AA1234567891011",
      "SubName": "SUBNAME-Tool"
    },
    {
      "name": "5678-PLACE-33",
      "OSType": "Windows",
      "CompName": "SERVER001",
      "RGName": "RG1234",
      "SubID": "AB1234567891011",
      "SubName": "SUBNAME-Tool"
    },
    {
      "name": "5678-PLACE-34",
      "OSType": "Windows",
      "CompName": "COMPUTER002",
      "RGName": "RG1234",
      "SubID": "AC1234567891011",
      "SubName": "SUBNAME-Tool"
    },
    {
      "name": "5678-PLACE-35",
      "OSType": "Windows",
      "CompName": "SERVER002",
      "RGName": "RG1234",
      "SubID": "AD1234567891011",
      "SubName": "SUBNAME-Tool"
    }
 ]
}

Here's my model Class:

public class Data
        {
            public string Name { get; set; }
            public string OSType { get; set; }
            public string CompName { get; set; }
            public string RGName { get; set; }
            public string SubID { get; set; }
            public string SubName { get; set; }
        }

Here is my Code:

using System;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;


namespace AzureWinWorkloadList
{
    class AzureWinWorkloadList
    {
        
        static void Main(string[] args)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("https://samm-prod-azfun.azurewebsites.net/api/...");
                
                var responseTask = client.GetAsync("");
                responseTask.Wait();

                var result = responseTask.Result;
                if (result.IsSuccessStatusCode)
                {
                    var readTask = result.Content.ReadAsAsync<Data[]>();
                    readTask.Wait();

                    var compnames = readTask.Result;
                    

                    foreach (var CompName in compnames)
                    {
                        Console.WriteLine(CompName.CompName);
                    }
                }
            }
            Console.ReadLine();
        }

This is the line where the error occurs

readTask.Wait();

Any guidance on this would be appreciated! Thanks in advance!

Aristos
  • 66,005
  • 16
  • 114
  • 150
ronniej962
  • 173
  • 1
  • 2
  • 10
  • 2
    Where's your class that holds the SkipToken and Data properties? (hint hint) – gunr2171 May 19 '21 at 22:40
  • Your JSON is not representative of the type you are trying to deserialize. You can't just make up stuff. Luckily there are many, many services to help you convert JSON to the correct type, like https://quicktype.io/csharp. – Heretic Monkey May 19 '21 at 23:02
  • Please, do some basic searching for the exception and error message; there are many hits on this site. – Heretic Monkey May 19 '21 at 23:06
  • Does this answer your question? [Using JsonConvert.DeserializeObject to deserialize Json to a C# POCO class](https://stackoverflow.com/questions/11126242/using-jsonconvert-deserializeobject-to-deserialize-json-to-a-c-sharp-poco-class) – Heretic Monkey May 19 '21 at 23:07
  • 1
    FYI since C# 7.1 you can have an `async Task Main()` method so you can `await` like normal instead of Wait()/.Result – maccettura May 19 '21 at 23:41

1 Answers1

1

You need a wrapper class to deserialize the JSON data.

public class WrapperClass 
{
    public SkipTokenClass SkipToken { get; set; }
    public IEnumerable<Data> Data { get; set; }
}
...
var readTask = result.Content.ReadAsAsync<WrapperClass>();
...
var wrapper = readTask.Result;

Use wrapper.Data to access the data.

Btw, aware that the property Name in Data class is different from the property name in the JSON data, consider use JsonPropertyAttribute.

[JsonProperty("name")]
public string Name { get; set; }
Gellio Gao
  • 835
  • 6
  • 19