0
\$\begingroup\$
[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?

\$\endgroup\$
3
  • \$\begingroup\$ I showed you a solution for this yesterday, where you only have to write the dictionary code once. \$\endgroup\$
    – DMGregory
    Mar 9, 2022 at 16:10
  • \$\begingroup\$ @DMGregory Yes, and thanks to you I wrote this code. But the thing is that I want to have a dictionary of all of the _contentLookups (in your example) and use it like this: MainDic["Story"]["Cave_Start"] but in your example, it would be MainDic.Story["Cave_Start"] \$\endgroup\$
    – Arian_ki
    Mar 9, 2022 at 16:15
  • \$\begingroup\$ A short notice: I've completely changed the code in the question according to a deleted answer \$\endgroup\$
    – Arian_ki
    Mar 9, 2022 at 18:58

1 Answer 1

0
\$\begingroup\$

The issue was a grave mistake in the JSON file, the structure I used was this:

{
  "Story":
  [
    {"Name":"FirstEncounter", "Content":[
      "sample",
      "sample2"
    ]},
    {"Name":"LaGrandDuel", "Content":[
      "sample",
      "sample2"
    ]}
  ],
  "Quest1":
  [
    {"Name":"FirstEncounter", "Content":[
      "sample",
      "sample2"
    ]},
    {"Name":"QuestDetail", "Content":[
      "sample",
      "sample2"
    ]}
  ]
}

The problem is that I use Story and Quest1 as Keys, not Values!

and so I changed the structure to this:

{
  "Main": [
    {
      "FamilyName": "Story",
      "Divisions": [
        {
          "Name": "JsonTest",
          "Content": [
            "sample",
            "sample2"
          ]
        },
        {
          "Name": "Jasper_First",
          "Content": [
            "sample",
            "sample2"
          ]
        },
        {
          "Name": "BlackSmith_Sword",
          "Content": [
            "sample",
            "sample2"
          ]
        }
      ]
    },
    {
      "FamilyName": "Quest1",
      "Divisions": [
        {
          "Name": "FirstEncounter",
          "Content": [
            "sample",
            "sample2"
          ]
        },
        {
          "Name": "QuestDetail",
          "Content": [
            "sample",
            "sample2"
          ]
        }
      ]
    }
  ]
}

And by doing this you will get rid of writing a new part of code for each Quest2, Quest3`, etc.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .