title: How to retrieve an unnamed json array
body: <p>I'm using this url:</p>
  
  <pre><code>https://jsonplaceholder.typicode.com/posts
  </code></pre>
  
  <p>I want to recover the title and body of this json, but when I pass</p>
  
  <pre><code>JSONArray postArrayJson = jsonObject.getJSONArray("");
  </code></pre>
  
  <p>It doesn't work... </p>
  
  <p>This is the function I'm using:</p>
  
  <pre><code>private List&lt;Post&gt; getPosts(JSONObject jsonObject) throws JSONException {
          List&lt;Post&gt; posts = new ArrayList&lt;&gt;();
  
          JSONArray postArrayJson = jsonObject.getJSONArray("");
          for (int i = 0; i &lt; postArrayJson.length(); i++) {
  
              JSONObject postArrayJsonJSONObject = postArrayJson.getJSONObject(i);
  
              String title = postArrayJsonJSONObject.getString("title");
              String body = postArrayJsonJSONObject.String("body");
  
              Post postsObj = new Post();
              postsObj.setTitle(body);
              postsObj.setBody(title);
              posts.add(postsObj);
  
          }
          return posts;
      }
  </code></pre>
  
  <p>I get her back here:</p>
  
  <pre><code>JSONObject jsonObject = new JSONObject(jsonAsString);
  
  List&lt;Post&gt; posts = getPosts(jsonObject);
  </code></pre>
  
  <p>All code: </p>
  
  <pre><code>public class JsonPostTask extends AsyncTask&lt;String, Void, List&lt;Post&gt;&gt; {
  
      private final WeakReference&lt;Context&gt; context;
      private ProgressDialog dialog;
      private PostLoader postLoader;
      private BufferedInputStream is;
  
      public JsonPostTask(Context context) {
          this.context = new WeakReference&lt;&gt;(context);
      }
  
      public void setPostLoader(PostLoader postLoader) {
          this.postLoader = postLoader;
      }
  
  
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
  
          Context context = this.context.get();
          if(context != null)
              dialog = ProgressDialog.show(context, "Carregando", "", true);
      }
  
      @Override
      protected List&lt;Post&gt; doInBackground(String... params) {
          String url = params[0];
  
          try {
              URL requestUrl = new URL(url);
              HttpsURLConnection urlConnection = (HttpsURLConnection) requestUrl.openConnection();
              urlConnection.setReadTimeout(2000);
              urlConnection.setConnectTimeout(2000);
  
              int responseCode = urlConnection.getResponseCode();
              if (responseCode &gt; 400)
                  throw new IOException("Erro na comunicação com o servidor remoto");
  
              InputStream inputStream = urlConnection.getInputStream();
  
              is = new BufferedInputStream(urlConnection.getInputStream());
  
              String jsonAsString = toString(is);
              JSONArray jsonObject = new JSONArray(jsonAsString);
  
  
  
              List&lt;Post&gt; posts = getPosts();
  
              is.close();
  
              return posts;
  
          } catch (Exception e) {
              e.printStackTrace();
          }
  
          return null;
      }
  
      private List&lt;Post&gt; getPosts() throws JSONException, IOException {
  
          String jsonAsString = toString(is);
  
          JSONArray postArrayJson = new JSONArray(jsonAsString);
  
          List&lt;Post&gt; posts = new ArrayList&lt;&gt;();
  
          for (int i = 0; i &lt; postArrayJson.length(); i++) {
  
              JSONObject postArrayJsonJSONObject = postArrayJson.getJSONObject(i);
              String title = postArrayJsonJSONObject.getString("title");
              String body = postArrayJsonJSONObject.getString("body");
              Post postsObj = new Post();
              postsObj.setTitle(body);
              postsObj.setBody(title);
              posts.add(postsObj);
          }
  
  
  
  
          return posts;
      }
  
      private String toString(InputStream inputStream) throws IOException {
          byte[] bytes = new byte[1024];
  
          ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
          int alreadyRead;
          while ((alreadyRead = inputStream.read(bytes)) &gt; 0) {
              byteArrayOutputStream.write(bytes, 0, alreadyRead);
          }
  
          return new String(byteArrayOutputStream.toByteArray());
      }
  
  
      @Override
      protected void onPostExecute(List&lt;Post&gt; posts) {
          super.onPostExecute(posts);
  
          dialog.dismiss();
          if (postLoader != null)
              postLoader.onResult(posts);
  
      }
  }
  </code></pre>