title: How to get the JSON code of a web page
body: <p>Am working on J2ee web application and am trying to get the code JSON of a web page via it's url in purpose to parse it later. I've already tried the code below:</p>
  
  <pre><code>          package so4308554;
  
  import java.io.BufferedReader;
  import java.io.IOException;``
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.Reader;
  import java.net.URL;
  import java.nio.charset.Charset;
  
  import org.json.JSONException;
  import org.json.JSONObject;
  
  public class JsonReader {
  
    private static String readAll(Reader rd) throws IOException {
      StringBuilder sb = new StringBuilder();
      int cp;
      while ((cp = rd.read()) != -1) {
        sb.append((char) cp);
      }
      return sb.toString();
    }
  
    public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
      InputStream is = new URL(url).openStream();
      try {
        BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
        String jsonText = readAll(rd);
        JSONObject json = new JSONObject(jsonText);
        return json;
      } finally {
        is.close();
      }
    }
  
    public static void main(String[] args) throws IOException, JSONException {
      JSONObject json = readJsonFromUrl("https://graph.facebook.com/19292868552");
      System.out.println(json.toString());
      System.out.println(json.get("id"));
    }
  }
  </code></pre>
  
  <p>But it works only with the given url if i put another one i get this exception:
  "A JSONObject text must begin with '{' at character 1"
   I think i can't get the Json content of web page if it is coded by html... any idea of what could be the causes??????? And how can i resolve my problem???</p>