Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6950961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:09:30+00:00 2026-05-27T14:09:30+00:00

I am trying to create a reddit app (just for practice) for Windows Phone

  • 0

I am trying to create a reddit app (just for practice) for Windows Phone 7 using C# and Json.Net. I am trying to get a hang of converting json to something c# can work with, along with some other things. I can pull in the data I want, but don’t know if I am using the correct methods in doing so. Then once I have the json data, I cant convert it to a custom object. Anyways, I’m sure there are tons of things wrong in this code so any help to straighten my heading would be much appreciated.

Here’s the code:

   public partial class MainPage : PhoneApplicationPage
    {
        string json = "";
    RootObject topic = new RootObject();
    public MainPage()
    {
        InitializeComponent();
    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        textBlock1.Text = "Retrieving...";
        string url = @"http://www.reddit.com/r/all.json";
        HttpWebRequest hWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
        hWebRequest.Method = "GET";
        hWebRequest.BeginGetResponse(Response_Completed, hWebRequest);
    }
    public void Response_Completed(IAsyncResult result)
    {
        HttpWebRequest request = (HttpWebRequest)result.AsyncState;
        HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);
        using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
        {
            json = streamReader.ReadToEnd();
            topic = JsonConvert.DeserializeObject<RootObject>(json);

        }
        Deployment.Current.Dispatcher.BeginInvoke(() =>
        {
            //textBlock2.Text = topic.data2.title;
            textBlock1.Text = "Done.";
        });

This line tells me that a “new” call is required.

textBlock2.Text = topic.data2.title;

Now this part is giving me the trouble. How do I instantiate data, data2, and child correctly?

     public class MediaEmbed
    {
    }

    public class Data2
    {
        public string domain { get; set; }
        public MediaEmbed media_embed { get; set; }
        public object levenshtein { get; set; }
        public string subreddit { get; set; }
        public string selftext_html { get; set; }
        public string selftext { get; set; }
        public object likes { get; set; }
        public bool saved { get; set; }
        public string id { get; set; }
        public bool clicked { get; set; }
        public string title { get; set; }
        public object media { get; set; }
        public int score { get; set; }
        public bool over_18 { get; set; }
        public bool hidden { get; set; }
        public string thumbnail { get; set; }
        public string subreddit_id { get; set; }
        public string author_flair_css_class { get; set; }
        public int downs { get; set; }
        public bool is_self { get; set; }
        public string permalink { get; set; }
        public string name { get; set; }
        public double created { get; set; }
        public string url { get; set; }
        public string author_flair_text { get; set; }
        public string author { get; set; }
        public double created_utc { get; set; }
        public int num_comments { get; set; }
        public int ups { get; set; }
        //public override string ToString()
        //{
        //    return title;
        //}
    }

    public class Child
    {
        public string kind { get; set; }
        public Data2 data { get; set; }
    }

    public class Data
    {
        public string modhash { get; set; }
        public Child[] children { get; set; }
        public string after { get; set; }
        public object before { get; set; }
    }

    public class RootObject
    {
        public RootObject()
        {
        }
        public string kind { get; set; }
        public Data data = new Data();
        public Data2 data2 = new Data2();
        public Child child = new Child();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:09:31+00:00Added an answer on May 27, 2026 at 2:09 pm

    I see a couple of problems with what you have. The first is the structure of the classes you have to deserialize the json data into. Manually writing up classes for this is really tedious and error-prone, and so I generally like to use this site to plug in the json and get out the classes I need and rename as appropriate. I’d then go and find the api docs for the data to fill in any gaps in the classes that the data simply just didn’t have.

    After that, you only need to deserialize a single instance of the root object class that you now have, rather than a List of them, so something like:

    RootObject topics = JsonConvert.DeserializeObject<RootObject>(json);
    

    One other note is that when you’re deserializing your data, in your code, you’re currently declaring a new local variable called topics which hides your member variable that is also called topics in that particular scope and so if you ever inspect that member variable, you would see that it won’t have any data in it. You don’t want to be declaring a new variable there, you just want to use the existing member variable, so instead of the previous line, you would just want to do:

    topics = JsonConvert.DeserializeObject<RootObject>(json);
    

    Or even this.topics if you want to be really explicit and clear about where that variable is coming from.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to deserialize some reddit comments (returned in JSON) using JSON.NET. I
I'm trying create an executable for Windows for a GUI application in tkinter using
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
Ok so I am trying create a login script, here I am using PHP5
Trying to create a QtRuby application, I get the following error: /usr/lib64/ruby/site_ruby/1.8/Qt/qtruby4.rb:2144: [BUG] Segmentation
Trying to create my first iPhone app that would play back audio. When I
Trying to create several layers of folders at once C:\pie\applepie\recipies\ without using several different
Trying to create a list to return some JSON data to a view. Following
hi I'm trying create chat using node.js I see example in http://chat.nodejs.org/ I have
I'm trying create a RCP Application with Eclipse, but I can't get past the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.