C
C#2y 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>
{

}
}
}
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; }
}
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!"
}]
}
],
"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!"
}
}
],
"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!"
}
}
],
4 Replies
Saber
Saber2y ago
So why is your property a Dictionary<ContentList, string> instead of just a ContentList
Angius
Angius2y ago
^ Seems like you didn't know whether to use ContentList or a Dictionary<string, string> so you tried using them both
Pdawg
Pdawg2y ago
Yeah, I tried using them both. I thought you needed both to make it work. Thanks!
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts