【问题标题】:'JsonPropertyAttribute' is inaccessible due to its protection level [duplicate]“JsonPropertyAttribute”由于其保护级别而无法访问[重复]
【发布时间】:2019-04-16 01:48:34
【问题描述】:

尝试解析 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 }
            },
        };
    }
}

以下是 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']"
        }
    ]
}

【问题讨论】:

标签: c# json


【解决方案1】:

由于其保护级别而无法访问 [...] 我尝试为 JsonProperty 添加公共构造函数,但它导致了另一个错误,即 JsonProperty 不是属性类。

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

在粘贴一些生成的代码时,您可能多次按 Ctrl+. 或 Alt+Enter 一次,从而导致 Visual Studio 或 ReSharper 为您生成此类。

只需从您的项目中删除该类即可。正确的lives in the Newtonsoft.Json namespace already。

【讨论】:

  • 在上面的代码中,公共构造函数被删除,然后上面的代码被发布,这导致了保护级别错误。我需要从上面的代码中删除任何其他内容吗?
  • 是的,就像我说的,你需要在你的项目中找到JsonPropertyAttribute这个类并删除它。
  • 好的,谢谢,试过了,但我的整个解决方案中没有 JsonPropertyAttribute 类。
  • 按 F12 om 属性,或右键单击 JsonProperty 并单击“转到定义”。
  • 这就是我得到的:public JsonPropertyAttribute();和公共 JsonPropertyAttribute(string propertyName);。此外,这些都存在于 Newtonsoft.json 命名空间中,并且无法对其进行编辑,因为它似乎已被锁定。
【解决方案2】:

有效的是通过 NuGet 包安装 Newtonsoft.json。显然即使它没有安装它仍然可以作为一个包导入,但需要安装。 希望这对遇到与我相同的错误的人有所帮助!

【讨论】:

    猜你喜欢
    • 2021-10-22
    • 2015-09-26
    • 2011-09-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-02
    • 2011-02-07
    相关资源
    最近更新 更多