Thursday, April 4, 2013

Parse Json list


namespace Endiem.CMS.Web.Classic.Pages
{
    /// <summary>
    /// Define the structure of facebook entity. In order to parse JSON.
    /// </summary>
    [DataContract]
    public class FacebookEntity
    {
        [DataMember(Name = "from")]
        public FacebooUser FBUser { get; set; }

        [DataMember(Name = "message")]
        public string FirstName { get; set; }

    }

    [DataContract]
    public class FacebookList
    {
        [DataMember(Name = "data")]
        public List<FacebookEntity> comments{ get; set; }
    }

    [DataContract]
    public class FacebooUser
    {
        [DataMember(Name = "name")]
        public string name { get; set; }

        [DataMember(Name = "id")]
        public string id { get; set; }

    }


    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CommentModule.EntityId = Guid.NewGuid();
            CommentModule.Entity = "article";
            CommentModule.MaxRows = 5;
            CommentModule.DbFactory = new Framework.DbContextFactory();

            string json = AskFacebbok("https://graph.facebook.com/comments/?ids=http://adamwebster.me/fbc.html");
            FacebookList li = Deserialise<FacebookList>(json);

         


        }


        public static T Deserialise<T>(string json)
        {
            T obj = Activator.CreateInstance<T>();
            using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
            {
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
                obj = (T)serializer.ReadObject(ms);
                return obj;
            }
        }

        public string AskFacebbok(string url)
        {
            string responseFromServer = String.Empty;
            WebResponse response = null;
            StreamReader reader = null;
            try
            {
                // Create a request for the URL.
                WebRequest request = WebRequest.Create(url);
                // If required by the server, set the credentials.
                request.Credentials = CredentialCache.DefaultCredentials;
                // Get the response.
                response = request.GetResponse();
                //// Display the status.
                //Console.WriteLine(((HttpWebResponse)response).StatusDescription);
                // Get the stream containing content returned by the server.
                Stream dataStream = response.GetResponseStream();
                // Open the stream using a StreamReader for easy access.
                reader = new StreamReader(dataStream);
                // Read the content.
                responseFromServer = reader.ReadToEnd();

            }
            catch (Exception)
            {

            }
            finally
            {
                reader.Close();
                response.Close();
            }
            return responseFromServer;
        }
    }
}

No comments:

Post a Comment