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!