Note that there are some explanatory texts on larger screens.

plurals
  1. POConverting json to custom object array
    text
    copied!<p>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. </p> <p>Here's the code:</p> <pre><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&lt;RootObject&gt;(json); } Deployment.Current.Dispatcher.BeginInvoke(() =&gt; { //textBlock2.Text = topic.data2.title; textBlock1.Text = "Done."; }); </code></pre> <p>This line tells me that a "new" call is required.</p> <pre><code>textBlock2.Text = topic.data2.title; </code></pre> <p>Now this part is giving me the trouble. How do I instantiate data, data2, and child correctly?</p> <pre><code> 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(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload