繁体   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