Rahul Jha
Rahul Jha

Reputation: 1141

Deserializing complex JSON using C#

public class Rootobject
{
    public EnPickthall enpickthall { get; set; }
}

public class EnPickthall
{
    public _1 _1 { get; set; }
    public _2 _2 { get; set; }
    public _3 _3 { get; set; }
    public _4 _4 { get; set; }
    /* This goes on*/
    public _6236 _6236 { get; set;}
}

 //For Each of the above properties a separate class has been defined:
public class _1
{
public int id { get; set; }
public int surah { get; set; }
public int ayah { get; set; }
public string verse { get; set; }
}

public class _2
{
public int id { get; set; }
public int surah { get; set; }
public int ayah { get; set; }
public string verse { get; set; }
}
/* So On for all the properties */

I got this via JSON2CSHARP! My problem is if I employ so many properties retrieving all Verses based upon their "Surah" would be very difficult & Impractical

Here I have a book in EnPickthall class which has a separate Class for every verse. Every Verse here has it's own class.

I have been scavenging Stack Overflow for hours.Is there any way I could simplify this JSON Classes.

My Code to Creating the object model :

        var serializer = new DataContractJsonSerializer(typeof(RootObject_Quran));
        var result= App_Code.FileIOHelper.ReadFromDefaultFile("ms-appx:///Assets/en.pickthall.json");

        var ms = new MemoryStream(Encoding.UTF8.GetBytes(result));

        var data = (RootObject_Quran)serializer.ReadObject(ms);

My JSON File Link : http://globalquran.com/download/data/download.php?data_by=json&quran_by_id[]=en.pickthall

Upvotes: 1

Views: 149

Answers (2)

user5775067
user5775067

Reputation:

Json is nested with property en.pickthall than all data wrapper again into property like id numbers so i made class and that class has Dictionary to handle data and numbers
[JsonProperty(PropertyName = "en.pickthall")]
public Dictionary picthall {get;set;}

    public class VerseObject
    {

        [JsonProperty(PropertyName = "en.pickthall")]
        public Dictionary<int, Data> picthall {get;set;}


    }
    public class Data
    {
        [JsonProperty(PropertyName = "id")]
        public int id;
        [JsonProperty(PropertyName = "surah")]
        public int surah;
        [JsonProperty(PropertyName = "ayah")]
        public int ayah;
        [JsonProperty(PropertyName = "verse")]
        public string verse;

    }

    class Program
    {
        static void Main(string[] args)
        {
            List<Data> v = new List<Data>();
            using (StreamReader rdr = new StreamReader(@"C:\Temp\en.pickthall.json"))
            {
                var str = rdr.ReadToEnd();
                var jsn = JsonConvert.DeserializeObject<VerseObject>(str);
                foreach(var item in jsn.picthall.Select(x=>x.Value))
                {
                    v.Add(item);
                }
                Console.ReadLine();
            }
        }


    }

Upvotes: 0

ManoDestra
ManoDestra

Reputation: 6503

Just have a single class called Verse.

public class Verse {
    public int SurahId { get; set; }

    public int AyaId { get; set; }

    public String Text { get; set; }
}

You don't really need a key for each verse, as the surah/aya combination is sufficient to uniquely identify a verse.

This allows for easy serialization/deserialization and also allows for easy gathering into the entire Quran again, by ordering on Surah and Aya. You could then simply use LINQ to reassemble the entire book, ordered as state above. Or it would be incredibly simple to gather passages from it also, based on a search criteria e.g. 27:18-20

Upvotes: 1

Related Questions