Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>Note: this answer has not been updated since 2013. The suggested way to download the json is not recommended for android anymore as the http client has been removed from the sdk as of api 23.<br> However, <strong>the parsing logic below still applies</strong></p> </blockquote> <hr> <p>Android has all the tools you need to parse json built-in. Example follows, no need for GSON or anything like that.</p> <p><strong>Get your JSON:</strong></p> <pre><code>DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams()); HttpPost httppost = new HttpPost(http://someJSONUrl/jsonWebService); // Depends on your web service httppost.setHeader("Content-type", "application/json"); InputStream inputStream = null; String result = null; try { HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); inputStream = entity.getContent(); // json is UTF-8 by default BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } result = sb.toString(); } catch (Exception e) { // Oops } finally { try{if(inputStream != null)inputStream.close();}catch(Exception squish){} } </code></pre> <p>now you have your JSON, so what?</p> <p><strong>Create a <a href="https://developer.android.com/reference/org/json/JSONObject.html" rel="noreferrer">JSONObject</a>:</strong></p> <pre><code>JSONObject jObject = new JSONObject(result); </code></pre> <p><strong>To get a specific string</strong></p> <pre><code>String aJsonString = jObject.getString("STRINGNAME"); </code></pre> <p><strong>To get a specific boolean</strong></p> <pre><code>boolean aJsonBoolean = jObject.getBoolean("BOOLEANNAME"); </code></pre> <p><strong>To get a specific integer</strong></p> <pre><code>int aJsonInteger = jObject.getInt("INTEGERNAME"); </code></pre> <p><strong>To get a specific long</strong></p> <pre><code>long aJsonLong = jObject.getLong("LONGNAME"); </code></pre> <p><strong>To get a specific double</strong></p> <pre><code>double aJsonDouble = jObject.getDouble("DOUBLENAME"); </code></pre> <p><strong>To get a specific <a href="http://developer.android.com/reference/org/json/JSONArray.html" rel="noreferrer">JSONArray</a>:</strong></p> <pre><code>JSONArray jArray = jObject.getJSONArray("ARRAYNAME"); </code></pre> <p><strong>To get the items from the array</strong></p> <pre><code>for (int i=0; i &lt; jArray.length(); i++) { try { JSONObject oneObject = jArray.getJSONObject(i); // Pulling items from the array String oneObjectsItem = oneObject.getString("STRINGNAMEinTHEarray"); String oneObjectsItem2 = oneObject.getString("anotherSTRINGNAMEINtheARRAY"); } catch (JSONException e) { // Oops } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload