Package test; 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;/*** java get json object according to the url * @ author openks * @ since 2013-7-16 * need to add java-json.jar to run */public class GetPlaceByIp {private static String re AdAll (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 ();/ /System. out. println ("at the same time, it can be seen from here that even if return is returned, it will still execute finally! ") ;}} Public static void main (String [] args) throws IOException, JSONException {// here call Baidu ip location api service for details see http://api.map.baidu.com/lbsapi/cloud/ip-location-api.htm JSONObject json = readJsonFromUrl (" http://api.map.baidu.com/location/ip? Ak = f454f8a5efe5e577997931cc01de00004 & ip = 202.198.16.3 "); System. out. println (json. toString (); System. out. println (JSONObject) json. get ("content ")). get ("address "));}}