C# ';JsonPropertyAttribute';由于其保护级别,无法访问

C# ';JsonPropertyAttribute';由于其保护级别,无法访问,c#,json,C#,Json,试图解析JSON文件时,我发现错误“JsonPropertyAttribute”由于其保护级别而无法访问。 应修改哪些内容以删除此错误 我尝试为JsonProperty添加公共构造函数,但它导致了另一个错误,即JsonProperty不是属性类 using System; using System.Collections.Generic; using System.Globalization; using Newtonsoft.Json; using Newtonsoft.Json.Conver

试图解析JSON文件时,我发现错误“JsonPropertyAttribute”由于其保护级别而无法访问。 应修改哪些内容以删除此错误

我尝试为JsonProperty添加公共构造函数,但它导致了另一个错误,即JsonProperty不是属性类

using System;
using System.Collections.Generic;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;


namespace NordstromRack
{
    public partial class ReadJson
    {
        [JsonProperty("items")]
        public Item[] Items { get; set; }
    }

    public partial class Item
    {
        [JsonProperty("url")]
        public Uri Url { get; set; }

        [JsonProperty("item_xpath", NullValueHandling = NullValueHandling.Ignore)]
        public string ItemXpath { get; set; }

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

    public partial class ReadJson
    {
        public static ReadJson FromJson(string json) => JsonConvert.DeserializeObject<ReadJson>(json, QuickType.Converter.Settings);
    }

    public static class Serialize
    {
        public static string ToJson(this ReadJson self) => JsonConvert.SerializeObject(self, QuickType.Converter.Settings);
    }

    internal static class Converter
    {
        public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
        {
            MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
            DateParseHandling = DateParseHandling.None,
            Converters =
            {
                new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
            },
        };
    }
}
由于其保护级别而无法访问[…]我尝试为JsonProperty添加公共构造函数,但它导致了另一个错误,即JsonProperty不是属性类

您似乎出于某种原因创建了自己的
JsonPropertyAttribute
类。它是不可访问的,因为它没有可访问性修饰符,因此不是公共的

您可能按了Ctrl+。或者,在粘贴某些生成的代码时,Alt+输入的次数过多,导致Visual Studio或ReSharper为您生成此类


只需从项目中删除该类。正确的方法。

有效的方法是通过NuGet软件包安装Newtonsoft.json。显然,即使没有安装,也可以作为包导入,但需要安装。
希望这能帮助那些像我一样面对同样错误的人

在上面的代码中,删除了公共构造函数,然后发布了上面的代码,这导致了保护级别错误。我需要从上述代码中删除任何其他内容吗?是的,正如我所说,您需要在项目中找到类
JsonPropertyAttribute
,然后将其删除。好的,谢谢,尝试过了,但我的整个解决方案中没有JsonPropertyAttribute类。从属性中点击F12,或者右键单击JsonProperty并单击“转到定义”.这就是我得到的:publicJSonPropertyAttribute();和公共JsonPropertyAttribute(字符串propertyName);。另外,这些都存在于Newtonsoft.json命名空间中,并且无法编辑它,因为它似乎已被锁定。摆脱它,改为使用Newtonsoft.Json执行
在您的c#代码中。见:谢谢@dbc。我复制了与您在我的中修复编译的代码完全相同的代码,但我仍然看到错误“JsonProperty”不是和属性类。JsonSerializerSettings、JsonConvert、DateParseHandling、NullValueHandling的错误与此相同。然后您可能需要添加对Json.NET的引用,请参阅。也可能会有所帮助。有效的方法是通过NuGet软件包安装Newtonsoft.json。显然,即使没有安装,它仍然可以作为一个包导入,但需要安装。很高兴您的问题得到解决。然后呢?
{
    "items": [{
            "url": "https://www.nordstromrack.com/shop/Women/Clothing/Activewear/Jackets%20&%20Hoodies",
            "item_xpath": "//*[@href='/shop/product/2299794/nike-vintage-drawstring-hoodie?color=BLACK%2FSAIL']",
            "item_size": "//*[@href='?color=TRUBER%2FSAIL&size=L']"
        },

        {
            "url": "https://www.nordstromrack.com/shop/product/2843153/blu-pepper-leopard-tie-sleeve-dress?color=LAVENDER",
            "item_size": "//*[@href='?color=LAVENDER&size=L']"
        },
        {
            "url": "https://www.nordstromrack.com/events/281375/products/2584102/j-crew-cotton-cardigan?color=BLACK",
            "item_xpath": "//*[@href='/events/281375/products/2584102/j-crew-cotton-cardigan?color=BLACK']",
            "item_size": "//*[@href='?color=BLACK&size=M']"
        }
    ]
}