简体   繁体   English

将 JSON 反序列化为 C# class 返回 Z37A6259CC0C1DAE299ZA7866489DFFBD0

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

I'm trying to deserialize a JSON file to a c# class.我正在尝试将 JSON 文件反序列化为 c# class。 However, my deserialize method always returns null.但是,我的反序列化方法总是返回 null。 My JSON file looks like this-我的 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."
    }
  ]
}

I'm trying to serialize it to below class-我正在尝试将其序列化为以下类别-

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

And finally, I'm using this code to deserialize it, however it returns null for some reason.最后,我使用此代码对其进行反序列化,但由于某种原因它返回 null。 Can someone please help?有人可以帮忙吗?

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

Cause原因

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. 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.

How do I know that?我怎么知道? Newtonsoft.Json does not have a JsonSerializer.Deserialize overload which takes a single string, and .NET does not contain a JsonPropertyAttribute . Newtonsoft.Json 没有采用单个字符串的JsonSerializer.Deserialize重载,并且 .NET 不包含JsonPropertyAttribute

Those two are not compatible.这两个不兼容。 The .NET deserializer ignores your [JsonProperty("Products")] attribute, does not find a propery named ProductsProducts in your JSON and, thus, yields null for that property. .NET 反序列化器会忽略您的[JsonProperty("Products")]属性,在您的 JSON 中找不到名为ProductsProducts的属性,因此会为该属性生成 Z37A6259CC0C1DAE29BD9A7866489DFF。


Fix使固定

To use the deserializer from the Newtonsoft.Json package instead, replace要改用 Newtonsoft.Json package 中的解串器,请替换

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

with

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

Here is a working fiddle with your code: https://dotnetfiddle.net/27Tz4t这是您的代码的工作小提琴: https://dotnetfiddle.net/27Tz4t

Using NewtonsoftJson使用NewtonsoftJson

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

Using System.Text.Json使用 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; }
}

if you have the json response, only use this site for convert Json to c# class如果您有 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;
        }

or use visual studio option Edit-->Paste Special-->Paste JSON as Classes或使用 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