Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi guys,

i have asp.net web site,
where i'm fetching data in JSon Format.

for which i have created a class..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BusinessLayer
{
    
        public class Outlet
        {
            public string id { get; set; }
            public string key { get; set; }
            public string title { get; set; }
        }

        public class Service
        {
            public string type { get; set; }
            public string id { get; set; }
            public string key { get; set; }
            public string title { get; set; }
            public List<Outlet> outlets { get; set; }
        }

        public class Image
        {
            public string pid { get; set; }
        }

        public class DisplayTitles
        {
            public string title { get; set; }
            public string subtitle { get; set; }
        }

        public class Service2
        {
            public string type { get; set; }
            public string id { get; set; }
            public string key { get; set; }
            public string title { get; set; }
        }

        public class Ownership
        {
            public Service2 service { get; set; }
        }

        public class Image2
        {
            public string pid { get; set; }
        }

        public class Image3
        {
            public string pid { get; set; }
        }

        public class Service3
        {
            public string type { get; set; }
            public string id { get; set; }
            public string key { get; set; }
            public string title { get; set; }
        }

        public class Ownership2
        {
            public Service3 service { get; set; }
        }

        public class Programme3
        {
            public string type { get; set; }
            public string pid { get; set; }
            public string title { get; set; }
            public object position { get; set; }
            public Image3 image { get; set; }
            public object expected_child_count { get; set; }
            public string first_broadcast_date { get; set; }
            public Ownership2 ownership { get; set; }
        }

        public class Programme2
        {
            public string type { get; set; }
            public string pid { get; set; }
            public string title { get; set; }
            public int position { get; set; }
            public Image2 image { get; set; }
            public int expected_child_count { get; set; }
            public string first_broadcast_date { get; set; }
            public Programme3 programme { get; set; }
        }

        public class Programme
        {
            public string type { get; set; }
            public string pid { get; set; }
            public int? position { get; set; }
            public string title { get; set; }
            public string short_synopsis { get; set; }
            public string media_type { get; set; }
            public int duration { get; set; }
            public Image image { get; set; }
            public DisplayTitles display_titles { get; set; }
            public string first_broadcast_date { get; set; }
            public Ownership ownership { get; set; }
            public Programme2 programme { get; set; }
            public bool is_available_mediaset_pc_sd { get; set; }
            public bool is_legacy_media { get; set; }
        }

        public class Broadcast
        {
            public bool is_repeat { get; set; }
            public bool is_blanked { get; set; }
            public string schedule_date { get; set; }
            public string start { get; set; }
            public string end { get; set; }
            public int duration { get; set; }
            public Service service { get; set; }
            public Programme programme { get; set; }
        }

        public class RootObject
        {
            public int page { get; set; }
            public int total { get; set; }
            public int offset { get; set; }
            public List<Broadcast> broadcasts { get; set; } // how can i get this list
        }
    
}


i want to fetch list<boardcast>,
for which i tried so far is,
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLayer;
using Newtonsoft.Json;
using System.Web.Script.Serialization;

namespace WebApplication
{
    public partial class GetJSon : System.Web.UI.Page
    {
        JsonBL bl = new JsonBL();
        RootObject oRootObject = new RootObject();        

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string url = "http://www.abc.co.uk/tv/programmes/genres/drama/scifiandfantasy/schedules/upcoming.json";
                string strJSon = bl.JSonData(url);
                JavaScriptSerializer oJS = new JavaScriptSerializer();
                oRootObject = oJS.Deserialize<RootObject>(strJSon);
            }
        }

        public void GetData()
        {
            //list<boardcast> code
        }
    }
}


can any one please help me to get ...

Thanks
Posted
Updated 24-Mar-15 6:14am
v5
Comments
F-ES Sitecore 24-Mar-15 4:41am    
Help you do what? What's the problem?
abdul subhan mohammed 24-Mar-15 6:45am    
In the RootObject class i'm calling Boardcast class,
n i have created instance of RootObject also...
but now the problem is, how can i use the properties of Boardcast class.

I want to know this. Plz.
F-ES Sitecore 24-Mar-15 10:12am    
foreach (Broadcast b in oRootObject.broadcasts)
{
// you can now access b.is_repeat etc
}
phil.o 24-Mar-15 10:14am    
Please show the code corresponding to this assertion (where you "call" the Broadcast class).
abdul subhan mohammed 24-Mar-15 11:58am    
i want to access all the class members of each class, where Programme is called in Boardcast and this Boardcast is then called in RootObject, as 'List'.

1 solution

Generally, you can "fetch" type members in a type-agnostic ways using reflection:
http://en.wikipedia.org/wiki/Reflection_%28computer_programming%29[^],
https://msdn.microsoft.com/en-us/library/f7ykdhsy%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.type%28v=vs.110%29.aspx[^].

However, if you need this for serializing your data with JSON, it is very likely that what you are looking for is already done for you, using System.Reflection (and, importantly, System.Reflection.Emit, for the sake of performance). Please see:
https://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/ms733127%28v=vs.110%29.aspx[^].

This is a very neat, non-intrusive, easiest to use and most robust way of doing serialization.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900