[System.Serializable]
public class JsonList
{
public string Name;
public List<string> Content;
}
[System.Serializable]
public class Root
{
public List<JsonList> Story;
public List<JsonList> Quest1;
Dictionary<string, Dictionary<string, List<string>>> _contentLookup;
public Dictionary<string, List<string>> this[string name]
{
get
{
return GetContent(name);
}
}
public Dictionary<string, List<string>> GetContent(string name)
{
return _contentLookup[name];
}
public void CreateDic()
{
_contentLookup = new Dictionary<string, Dictionary<string, List<string>>> { };
foreach (var item in Story)
{
if (!_contentLookup.ContainsKey(nameof(Story)))
_contentLookup.Add(nameof(Story), new Dictionary<string, List<string>> { });
_contentLookup[nameof(Story)].Add(item.Name, item.Content);
}
}
}
Let's say I have Root a;
, so I would access different List<string>
s like this: a["Story"]["Cave_start"]
(assume I've called a.CreateDic()
and all of the keys such as "Story" exist)
The problem is that it's not efficient. if I want to access Quest1
then I have to manually add
foreach (var item in Quest1)
{
if (!_contentLookup.ContainsKey(nameof(Quest1)))
_contentLookup.Add(nameof(Quest1), new Dictionary<string, List<string>> { });
_contentLookup[nameof(Quest1)].Add(item.Name, item.Content);
}
in CreateDic()
Obviously, this is nothing I would desire to do for Quest2, Quest3, HiddenLevel1, etc. in Root
.
Any idea how to make it better?
_contentLookup
s (in your example) and use it like this:MainDic["Story"]["Cave_Start"]
but in your example, it would beMainDic.Story["Cave_Start"]
\$\endgroup\$