I am currently trying to parse through a the front page of Reddit using their cool website feature in which you can add /.json to any site to get the json of the page. So the url I am using is www.reddit.com/.json.
I want to get the subreddit of the first post by parsing through their json. How would I do this? I did some research and found the google gson api but I have no idea how to use it and their documentation does not really help me.
Here is my code so far, I have the Json in a string:
import java.io.*;
import java.net.*;
import com.google.gson.*;
public class Subreddits {
public static void main(String[] args) {
URL u = null;
try {
u = new URL("http://www.reddit.com/.json");
} catch (MalformedURLException e) {
e.printStackTrace();
}
URLConnection yc = null;
try {
yc = u.openConnection();
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
} catch (IOException e) {
e.printStackTrace();
}
String inputLine = null;
StringBuilder sb = new StringBuilder();
try {
while ((inputLine = in.readLine()) != null){
sb.append(inputLine);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
inputLine = sb.toString();//String of json
System.out.println(inputLine);
//I want to get [data][children][data][subreddit]
}
}