簡體   English   中英

將 JSON 反序列化為 C# class 返回 Z37A6259CC0C1DAE299ZA7866489DFFBD0

[英]Deserializing JSON to a C# class returns null

我正在嘗試將 JSON 文件反序列化為 c# class。 但是,我的反序列化方法總是返回 null。 我的 JSON 文件看起來像這樣-

{
  "Products": [
    {
      "ProductID": 994,
      "Name": "LL Bottom Bracket",
      "ProductNumber": "BB-7421",
      "ProductCategoryID": 9,
      "ProductCategory": "Bottom Brackets",
      "ProductModelID": 95,
      "Description": "Chromoly steel."
    },
    {
      "ProductID": 995,
      "Name": "ML Bottom Bracket",
      "ProductNumber": "BB-8107",
      "ProductCategoryID": 9,
      "ProductCategory": "Bottom Brackets",
      "ProductModelID": 96,
      "Description": "Aluminum alloy cups; large diameter spindle."
    }
  ]
}

我正在嘗試將其序列化為以下類別-

public class Product
    {
        [JsonProperty("ProductID")]
        public long ProductId { get; set; }

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

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

        [JsonProperty("ProductCategoryID")]
        public long ProductCategoryId { get; set; }

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

        [JsonProperty("ProductModelID")]
        public long ProductModelId { get; set; }

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

        [JsonProperty("Color", NullValueHandling = NullValueHandling.Ignore)]
        public string Color { get; set; }
    }
    public partial class Products
    {
        [JsonProperty("Products")]
        public IEnumerable<Product> ProductsProducts { get; set; }
    }

最后,我使用此代碼對其進行反序列化,但由於某種原因它返回 null。 有人可以幫忙嗎?

public Products Get()
        {
            var jsonString = IO.File.ReadAllText("ProductsData.json");
            var products = JsonSerializer.Deserialize<Products>(jsonString);
            
            return products;
        }

原因

You are using the JsonProperty attribute from the Netwonsoft.Json package, but the built-in .NET Core/5 deserializer from the System.Text.Json namespace.

我怎么知道? Newtonsoft.Json 沒有采用單個字符串的JsonSerializer.Deserialize重載,並且 .NET 不包含JsonPropertyAttribute

這兩個不兼容。 .NET 反序列化器會忽略您的[JsonProperty("Products")]屬性,在您的 JSON 中找不到名為ProductsProducts的屬性,因此會為該屬性生成 Z37A6259CC0C1DAE29BD9A7866489DFF。


使固定

要改用 Newtonsoft.Json package 中的解串器,請替換

var products = JsonSerializer.Deserialize<Products>(jsonString);

var products = JsonConvert.DeserializeObject<Products>(jsonString);

這是您的代碼的工作小提琴: https://dotnetfiddle.net/27Tz4t

使用NewtonsoftJson

var products = JsonConvert.DeserializeObject<Products>(jsonString);

使用 System.Text.Json

var products = JsonSerializer.Deserialize<Products>(jsonString);
public partial class Products
{
    [System.Text.Json.Serialization.JsonPropertyName("Products")]
    public IEnumerable<Product> ProductsProducts { get; set; }
}

如果您有 json 響應,請僅使用此站點將Json 轉換為 c# ZA2F2ED4F8EBC2CBB14C21A29DC04

// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse); 
    public class Product    {
        public int ProductID { get; set; } 
        public string Name { get; set; } 
        public string ProductNumber { get; set; } 
        public int ProductCategoryID { get; set; } 
        public string ProductCategory { get; set; } 
        public int ProductModelID { get; set; } 
        public string Description { get; set; } 
    }

    public class Root    {
        public List<Product> Products { get; set; } 
    }

public Products Get()
        {
            var jsonString = IO.File.ReadAllText("ProductsData.json");
            var products = JsonSerializer.Deserialize<Root>(jsonString);
            
            return products;
        }

或使用 Visual Studio 選項Edit-->Paste Special-->Paste JSON as Classes

 public class Rootobject
        {
            public Product[] Products { get; set; }
        }

        public class Product
        {
            public int ProductID { get; set; }
            public string Name { get; set; }
            public string ProductNumber { get; set; }
            public int ProductCategoryID { get; set; }
            public string ProductCategory { get; set; }
            public int ProductModelID { get; set; }
            public string Description { get; set; }
        }




public Products Get()
            {
                var jsonString = IO.File.ReadAllText("ProductsData.json");
                var products = JsonSerializer.Deserialize<Rootobject>(jsonString);
                
                return products;
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM