Simplest way to read json from a URL in java

后端 未结 11 931
情书的邮戳
情书的邮戳 2020-11-22 04:15

This might be a dumb question but what is the simplest way to read and parse JSON from URL in Java?

In Groovy, it\

相关标签:
11条回答
  • 2020-11-22 04:54

    The easiest way: Use gson, google's own goto json library. https://code.google.com/p/google-gson/

    Here is a sample. I'm going to this free geolocator website and parsing the json and displaying my zipcode. (just put this stuff in a main method to test it out)

        String sURL = "http://freegeoip.net/json/"; //just a string
    
        // Connect to the URL using java's native library
        URL url = new URL(sURL);
        URLConnection request = url.openConnection();
        request.connect();
    
        // Convert to a JSON object to print data
        JsonParser jp = new JsonParser(); //from gson
        JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent())); //Convert the input stream to a json element
        JsonObject rootobj = root.getAsJsonObject(); //May be an array, may be an object. 
        String zipcode = rootobj.get("zip_code").getAsString(); //just grab the zipcode
    
    0 讨论(0)
  • 2020-11-22 04:57

    Here's a full sample of how to parse Json content. The example takes the Android versions statistics (found from Android Studio source code here, which links to here).

    Copy the "distributions.json" file you get from there into res/raw, as a fallback.

    build.gradle

        implementation 'com.google.code.gson:gson:2.8.6'
    

    manifest

      <uses-permission android:name="android.permission.INTERNET" />
    

    MainActivity.kt

    class MainActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            if (savedInstanceState != null)
                return
            thread {
                // https://cs.android.com/android/platform/superproject/+/studio-master-dev:tools/adt/idea/android/src/com/android/tools/idea/stats/DistributionService.java
                var root: JsonArray
                Log.d("AppLog", "loading...")
                try {
                    HttpURLConnection.setFollowRedirects(true)
                    val statsUrl = "https://dl.google.com/android/studio/metadata/distributions.json" //just a string
                    val url = URL(statsUrl)
                    val request: HttpURLConnection = url.openConnection() as HttpURLConnection
                    request.connectTimeout = 3000
                    request.connect()
                    InputStreamReader(request.content as InputStream).use {
                        root = JsonParser.parseReader(it).asJsonArray
                    }
                } catch (e: Exception) {
                    Log.d("AppLog", "error while loading from Internet, so using fallback")
                    e.printStackTrace()
                    InputStreamReader(resources.openRawResource(R.raw.distributions)).use {
                        root = JsonParser.parseReader(it).asJsonArray
                    }
                }
                val decimalFormat = DecimalFormat("0.00")
                Log.d("AppLog", "result:")
    
                root.forEach {
                    val androidVersionInfo = it.asJsonObject
                    val versionNickName = androidVersionInfo.get("name").asString
                    val versionName = androidVersionInfo.get("version").asString
                    val versionApiLevel = androidVersionInfo.get("apiLevel").asInt
                    val marketSharePercentage = androidVersionInfo.get("distributionPercentage").asFloat * 100f
                    Log.d("AppLog", "\"$versionNickName\" - $versionName - API$versionApiLevel - ${decimalFormat.format(marketSharePercentage)}%")
                }
            }
        }
    }
    

    As alternative to the dependency, you can also use this instead:

    InputStreamReader(request.content as InputStream).use {
        val jsonArray = JSONArray(it.readText())
    }
    

    and the fallback:

    InputStreamReader(resources.openRawResource(R.raw.distributions)).use {
        val jsonArray = JSONArray(it.readText())
    }
    

    The result of running this:

    loading...
    result:
    "Ice Cream Sandwich" - 4.0 - API15 - 0.20%
    "Jelly Bean" - 4.1 - API16 - 0.60%
    "Jelly Bean" - 4.2 - API17 - 0.80%
    "Jelly Bean" - 4.3 - API18 - 0.30%
    "KitKat" - 4.4 - API19 - 4.00%
    "Lollipop" - 5.0 - API21 - 1.80%
    "Lollipop" - 5.1 - API22 - 7.40%
    "Marshmallow" - 6.0 - API23 - 11.20%
    "Nougat" - 7.0 - API24 - 7.50%
    "Nougat" - 7.1 - API25 - 5.40%
    "Oreo" - 8.0 - API26 - 7.30%
    "Oreo" - 8.1 - API27 - 14.00%
    "Pie" - 9.0 - API28 - 31.30%
    "Android 10" - 10.0 - API29 - 8.20%
    
    0 讨论(0)
  • 2020-11-22 04:58

    Use HttpClient to grab the contents of the URL. And then use the library from json.org to parse the JSON. I've used these two libraries on many projects and they have been robust and simple to use.

    Other than that you can try using a Facebook API java library. I don't have any experience in this area, but there is a question on stack overflow related to using a Facebook API in java. You may want to look at RestFB as a good choice for a library to use.

    0 讨论(0)
  • 2020-11-22 04:58

    I am not sure if this is efficient, but this is one of the possible ways:

    Read json from url use url.openStream() and read contents into a string.

    construct a JSON object with this string (more at json.org)

    JSONObject(java.lang.String source)
          Construct a JSONObject from a source JSON text string.
    
    0 讨论(0)
  • 2020-11-22 05:03

    I have found this to be the easiest way by far.

    Use this method:

    public static String getJSON(String url) {
            HttpsURLConnection con = null;
            try {
                URL u = new URL(url);
                con = (HttpsURLConnection) u.openConnection();
    
                con.connect();
    
    
                BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
                StringBuilder sb = new StringBuilder();
                String line;
                while ((line = br.readLine()) != null) {
                    sb.append(line + "\n");
                }
                br.close();
                return sb.toString();
    
    
            } catch (MalformedURLException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            } finally {
                if (con != null) {
                    try {
                        con.disconnect();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            }
            return null;
        }
    

    And use it like this:

    String json = getJSON(url);
    JSONObject obj;
       try {
             obj = new JSONObject(json);
             JSONArray results_arr = obj.getJSONArray("results");
             final int n = results_arr.length();
                for (int i = 0; i < n; ++i) {
                    // get the place id of each object in JSON (Google Search API)
                    String place_id = results_arr.getJSONObject(i).getString("place_id");
                }
    
    
       }
    
    0 讨论(0)
  • 2020-11-22 05:03

    I wanted to add an updated answer here since (somewhat) recent updates to the JDK have made it a bit easier to read the contents of an HTTP URL. Like others have said, you'll still need to use a JSON library to do the parsing, since the JDK doesn't currently contain one. Here are a few of the most commonly used JSON libraries for Java:

    • org.JSON
    • FasterXML Jackson
    • GSON

    To retrieve JSON from a URL, this seems to be the simplest way using strictly JDK classes (but probably not something you'd want to do for large payloads), Java 9 introduced: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/io/InputStream.html#readAllBytes()

    try(java.io.InputStream is = new java.net.URL("https://graph.facebook.com/me").openStream()) {
        String contents = new String(is.readAllBytes());
    }
    

    To parse the JSON using the GSON library, for example

    com.google.gson.JsonElement element = com.google.gson.JsonParser.parseString(contents); //from 'com.google.code.gson:gson:2.8.6'
    
    0 讨论(0)
提交回复
热议问题