2014-02-15 166 views

回答

0

這裏是我從URL獲取JSON和打印出簡單的例子:

public static void main(String[] args) throws MalformedURLException, IOException { 
    String sURL = "your_url_goes_here"; 
    URL url = new URL(sURL); 
    HttpURLConnection request = (HttpURLConnection) url.openConnection(); 
    request.connect(); 

    // Convert to a JSON object to print data 
    JsonParser jp = new JsonParser(); //from gson 
    //Convert the input stream to a json element 
    JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent())); 
    JsonObject rootobj = root.getAsJsonObject(); //May be an array, may be an object. 

    System.out.println(rootobj.toString()); // json is printed 
} 
相關問題