Imp Montezuma
Imp Montezuma

Reputation: 3

Can I use a single json file to create a n number of instances of different classes?

Is possible to create (from the example below), lets say, 2 instances of Animal and 3 instances of Flower based on data from a single json file?

Example code:

class Nature
{
    // do something with a specific json file
    class Animal
    {
        string id;
        bool isMammal;
    }

    class Flower
    {
        string id;
        int numberOfPetals;
    }
}

Expected result:

PS:

x and y depends on the data obtained from the json file.

The workaround I thought was to, instead of creating an json file, create a .txt file, which contains Json data fragments. Then, save the contents of the .txt file into a variable. And finally, from that variable select each json data fragments to work with them individually, as if each one were an individual json file.

But, would it have a simpler way of doing this?

Upvotes: 0

Views: 322

Answers (2)

Albert Alberto
Albert Alberto

Reputation: 950

You can load the JSON file and convert the data into datatable. Create new list of Animal and Flower.

c# code as follows:

//Declare typed list of Animals
            List<Animal> AllAnimals = new List<Animal>();

            //Declare typed list of Flowers
            List<Flower> AllFlowers = new List<Flower>();
            DataTable dt;
            //Load JSON file data
            using (StreamReader r = new StreamReader(JSONFilePath))
            {
                string json = r.ReadToEnd();
                //convert JSON data to datatable
                dt = (DataTable)JsonConvert.DeserializeObject(json, (typeof(DataTable)));
            }
           foreach(DataRow row in dt.Rows)
            {
                if (row[1].ToString() == "Animal")
                {
                    Animal NewAnimal = new Animal();
                    NewAnimal.id = row[0].ToString();
                    NewAnimal.isMammal = row[2].ToString();
                    AllAnimals.Add(NewAnimal);
                }
                else
                {
                    Flower NewFlower = new Flower();
                    NewFlower.id = row[0].ToString();
                    NewFlower.numberOfPetals = row[3].ToString();
                    AllFlowers.Add(NewFlower);
                }
            }

Below is a sample of json data that can be loaded from a file:

{
    {
   "id": "0",
   "EntryType": "Animal",
   "IsMamal": "True",
   "numberOfPetals": ""
 },
 {
   "id": "1",
   "EntryType": "Animal",
   "IsMamal": "True",
   "numberOfPetals": ""
 },
 {
   "id": "2",
   "EntryType": "Flower",
   "IsMamal": "",
   "numberOfPetals": "8"
 },
 {
   "id": "1",
   "EntryType": "Flower",
   "IsMamal": "",
   "numberOfPetals": "6"
 },
 {
   "id": "2",
   "EntryType": "Flower",
   "IsMamal": "",
   "numberOfPetals": "10"
 }
}

Upvotes: 0

Manoj Choudhari
Manoj Choudhari

Reputation: 5634

You can use JSON structure instead of maintining the count.

{
   "AnimalCollection" :
            [
                 { "id":"Animal-1", "IsMammal":true },
                 { "id":"Animal-2", "IsMammal":true },
                 { "id":"Animal-3", "IsMammal":true }
            ],
    "FlowerCollection":
            [
                 { "id":"Flower-1", "numberOfPetals":30 },
                 { "id":"Flower-2", "numberOfPetals":20 },
                 { "id":"Flower-3", "numberOfPetals":10 },
                 { "id":"Flower-4", "numberOfPetals":3 }
            ]
}

Then you can deserialize this using newtonsoft.json into below type

public class Data
{
    public Animal[] AnimalCollection {get;set;}
    public Flower[] FlowerCollection{get;set;
}

It will contain 3 animal instances and 4 flower instances from JSON.

Hope this helps.

Upvotes: 1

Related Questions