C#C
C#4y ago
Pdawg

❔ How can I access a member of a different class in a dictionary

Hey! I'm trying to refactor my old JSON serializer code, but I ran into an issue. I can't access a string in another class named RssTitle. In this Content property, there are 3 different strings that I need to access, RssTitle, RssDesc, and RssPubDate. When I use a dictionary like this:

 Resources = new List<WebTileResource>
                {
                    new WebTileResource
                    {
            Url = ResourceURL,
            Style = ResourceType,
            Content = new Dictionary<ContentList, string>
                        {
                            
                        }
                    }
                }

It can't access those 3 strings, and just says that RssTitle does not exist in the current context. Here is the data model behind this code

public class WebTileResource
    {
        public string Url { get; set; }
        public string Style { get; set; }
        public Dictionary<ContentList, string> Content { get; set; }
    }

    public class ContentList
    {
        public string RssTitle { get; set; }
        public string RssDesc { get; set; }
        public string RssPubDate { get; set; }
    }

If I use a List, like this: public List<ContentList> Content { get; set; }, it creates an array in the serialized JSON. I need it to not be an array. Here's what the final JSON looks like:

"resources": [
{
"url": "some_rss_feed",
"style": "Simple",
"content": [{
"rssTitle": "Add any variables that you need here",
"rssDesc": "Add any variables that you need here",
"rssPubDate": "You can pull any of the data attributes from a selected data source!"
      }]
    }
  ],

I need it to look like this:
"resources": [
{
"url": "some_rss_feed",
"style": "Simple",
"content": {
"rssTitle": "Add any variables that you need here",
"rssDesc": "Add any variables that you need here",
"rssPubDate": "You can pull any of the data attributes from a selected data source!"
      }
    }
  ],
Was this page helpful?