-1

Trying to parse a JSON file and I am getting the error 'JsonPropertyAttribute' is inaccessible due to its protection level. What should be modified so as to remove this error?

I tried adding a public constructor for JsonProperty but it led to another error i.e. JsonProperty is not an attribute class.

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

Following is the JSON file

{
    "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']"
        }
    ]
}
Sayali Sheode
  • 59
  • 1
  • 2
  • 7
  • In your [other question](https://stackoverflow.com/q/55692935/3744182) you have `public class JsonProperty { }`. Get rid of that and instead do `using Newtonsoft.Json;` in your c# code. See: https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonPropertyAttribute.htm – dbc Apr 15 '19 at 16:45
  • Thanks @dbc. I copied the exact same code where you have fixed the compilation in my [other question](https://stackoverflow.com/questions/55692935/how-can-the-below-code-for-json-file-parsing-be-edited-to-as-to-be-more-efficien?noredirect=1#comment98071177_55692935) but I still see an error 'JsonProperty' is not and attribute class. Same error for JsonSerializerSettings, JsonConvert, DateParseHandling, NullValueHandling. – Sayali Sheode Apr 15 '19 at 17:26
  • Then you probably need to add a reference to Json.NET, see [JSON.NET How To Reference?](https://stackoverflow.com/q/9366985). [why cant i add the newtonsoft.Json.dll ref to my project?](https://stackoverflow.com/q/18230014) and [How to import JsonConvert in C# application?](https://stackoverflow.com/q/18784697) might also help. – dbc Apr 15 '19 at 17:32
  • What worked was to install Newtonsoft.json via NuGet packages. Apparently even though it is not installed it still can be imported as a package but installation is needed. – Sayali Sheode Apr 15 '19 at 17:36
  • 1
    Glad your problem got resolved. Duplicate of [JSON.NET How To Reference?](https://stackoverflow.com/q/9366985) and [How to import JsonConvert in C# application?](https://stackoverflow.com/q/18784697) then? – dbc Apr 15 '19 at 17:38
  • This issue isn't reproducible after you found that you simply had not installed the NuGet package (per your own answer). Often, these types of questions are closed since it just an oversight of normal procedure and process... I would say that's a likely reason for a downvote. – gravity Apr 17 '19 at 18:20

2 Answers2

1

is inaccessible due to its protection level [...] I tried adding a public constructor for JsonProperty but it led to another error i.e. JsonProperty is not an attribute class.

It appears you've created your own JsonPropertyAttribute class for some reason. It is inaccessible because it doesn't have an accessibility modifier and is therefore not public.

You probably hit Ctrl+. or Alt+Enter one time too many while pasting some generated code, resulting in Visual Studio or ReSharper generating this class for you.

Simply remove that class from your project. The proper one lives in the Newtonsoft.Json namespace already.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • In the above code the Public constructor was removed and then the above code was posted which is leading to the protection level error. Do I need to remove anything else from the above code? – Sayali Sheode Apr 15 '19 at 14:55
  • Yes, as I said, you need to find the class `JsonPropertyAttribute` in your project and remove it. – CodeCaster Apr 15 '19 at 15:02
  • Okay thanks, tried that but there is no JsonPropertyAttribute class in my entire solution. – Sayali Sheode Apr 15 '19 at 15:08
  • Hit F12 om the attribute, or right-click JsonProperty and click "Go to Definition". – CodeCaster Apr 15 '19 at 15:13
  • This is what I got: public JsonPropertyAttribute(); and public JsonPropertyAttribute(string propertyName);. Plus these are present in Newtonsoft.json namespace and cannot edit it as it seems to be locked down. – Sayali Sheode Apr 15 '19 at 15:39
0

What worked was to install Newtonsoft.json via NuGet packages. Apparently even though it is not installed it still can be imported as a package but installation is needed. Hope this helps anyone who faced the same error as I did!

Sayali Sheode
  • 59
  • 1
  • 2
  • 7