0

I have done something similar to this before however I'm not sure how to do this with a bigger project. I'm trying to return the titles of all the stuff on the front page of reddit.

From this site:

http://www.reddit.com/r/all.json

I pasted the data into

http://json2csharp.com/#

to find out the class I need.

From here though, I'm not too sure on how to proceed. If I wanted to return an array of all this data so I can easily get information, how could I do it. Sorry for the vagueness of this question but I'm just at a loss and don't know what to do.

ekad
  • 14,436
  • 26
  • 44
  • 46
Ayohaych
  • 5,099
  • 7
  • 29
  • 51

1 Answers1

0

Use

using (var webClient = new System.Net.WebClient()) {
    var json = webClient.DownloadString("http://www.reddit.com/r/all.json");
}

For old .Net:

var request = WebRequest.Create(url);
string text;
 request.ContentType = "application/json; charset=utf-8";

var response = (HttpWebResponse) request.GetResponse();

using (var sr = new StreamReader(response.GetResponseStream()))
{
    text = sr.ReadToEnd();
}
Community
  • 1
  • 1
  • I get an error on the new System.Net.WebClient, says that it doesn't exist in the namespace and I can't import it. – Ayohaych Jan 05 '14 at 17:43
  • 1
    In System.Net.WebClient –  Jan 05 '14 at 17:43
  • 1
    @AndyOHart http://msdn.microsoft.com/en-us/library/system.net.webclient(v=vs.110).aspx –  Jan 05 '14 at 17:44
  • .Net for me only has Http and NetworkInformation, cant import WebClient :S This is for a Windows store app so maybe thats why? – Ayohaych Jan 05 '14 at 17:50
  • 1
    @AndyOHart What is you .net vesrion –  Jan 05 '14 at 17:52
  • Thanks for that. Im getting an error on: var response = (HttpWebResponse)request.GetResponseAsync(); It says Cannot convert type 'System.Threading.Tasks.Task' to 'System.Net.HttpWebResponse' Any ideas? – Ayohaych Jan 05 '14 at 18:03
  • 1
    let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/44562/discussion-between-shahrooz-jefri--and-andyohart) –  Jan 05 '14 at 18:05