如何在c#中获取类及其属性 [英] how to get classes and its properties in c#

查看:89
本文介绍了如何在c#中获取类及其属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



i有asp.net网站,

我用JSon格式获取数据。



为此我创建了一个班级..



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想要获取列表< ; Boardcast>,
我到目前为止尝试的
是,



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

推荐答案

通常,您可以使用类型无关的方式获取类型成员反思

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 [ ^ ]。



但是,如果您需要使用JSON序列化数据,那很可能就是您要找的内容已经为你做了,使用 System.Reflection (重要的是, System.Reflection.Emit ,为了表现)。请参阅:

https://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer%28v=vs.110%29.aspx [ ^ ],

https:// msdn .microsoft.com / zh-CN / library / ms733127%28v = vs.110%29.aspx [ ^ ]。



这是一个非常整洁的,非侵入式,最易于使用和最强大的序列化方式。



-SA
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


这篇关于如何在c#中获取类及其属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆