0

I am trying to figure out if it is possible to parse multiple json files that are referencing each other into C# class structure.
Example:

Class json file

{
   "wizard" : 
   {
      "type" : "ice",
      "spells" : ["ice_orb", "ice_storm"]
   }
}

Spells json file:

{
   "spells" : 
   {
      "ice_orb":
      {
         "damage": 25,
         "cooldown": 2
      },
      "ice_storm":
      {
         "damage": 35,
         "cooldown": 4
      }
   }
}

Expected Result:

[Serializable]
public class IceOrb
{
    public int damage;
    public int cooldown;
}

[Serializable]
public class IceStorm
{
    public int damage;
    public int cooldown;
}

[Serializable]
public class Spell
{
    public IceOrb ice_orb;
    public IceStorm ice_storm;
}

[Serializable]
public class Wizard
{
    public string type;
    public List<Spell> spells;
}

Please do not suggest to merge everything in one file because it is not an option for me.

6
  • You may create partial classes to hold these. Commented Nov 7, 2016 at 7:11
  • you can convert the jsons to respected class and then can relate the class objects Commented Nov 7, 2016 at 7:17
  • @viveknuna, do you have a link to tutorial? Commented Nov 7, 2016 at 7:43
  • You can use Linq to join the relations after deserialization, but thats about as much automagic that I think you can get. edit: There is not point in having two classes with same behaviour Commented Nov 7, 2016 at 12:09
  • take a look at this: stackoverflow.com/a/11345478/366064 Commented Nov 7, 2016 at 14:29

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.