2014-02-15 125 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 
} 
相关问题