繁体   English   中英

控制台应用程序无法反序列化 JSON

[英]Console App Failing to Deserialize the JSON

执行代码时收到以下错误:

JsonSerializationException: Cannot deserialize the current JSON object (eg {"name":"value"}) into type 'AzureWinWorkloadList.AzureWinWorkloadList+Data[]' because the type requires a JSON array (eg [1,2,3]) to deserialize正确。 To fix this error either change the JSON to a JSON array (eg [1,2,3]) or change the deserialized type so that it is a normal .NET type (eg not a primitive type like integer, not a collection type like an数组或列表)可以从 JSON object 反序列化。 JsonObjectAttribute 也可以添加到类型中以强制它从 JSON object 反序列化。 路径“SkipToken”,第 2 行,position 14。

这是一个独立的控制台应用程序,不是 MVC 类型项目的一部分。 此外,这在几周前对我有用,但是当我回到它时,现在我得到了那个错误。

这是我的 JSON 的样子:

{
  "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"
    }
 ]
}

这是我的 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; }
        }

这是我的代码:

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();
        }

这是发生错误的行

readTask.Wait();

对此的任何指导将不胜感激! 提前致谢!

您需要一个包装器 class 来反序列化 JSON 数据。

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

使用wrapper.Data访问数据。

顺便说一句,知道Data class 中的属性Name与 JSON 数据中的属性name不同,请考虑使用JsonPropertyAttribute

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM