title: Getting an array from a JSON object in Java
body: <p>How would I go about parsing this JSON object and getting the "groups" array in Java on Android.</p>
  
  <pre><code>{ "groups": [ "Total_Social_Beta_Testers", "Test_Group" ] }
  </code></pre>
  
  <p>Here's my code: </p>
  
  <p><strong>LoginActivity.java</strong></p>
  
  <pre><code>    Response.Listener&lt;String&gt; responseListener = new Response.Listener&lt;String&gt;() {
  
  
          @Override
          public void onResponse(String response) {
              try {
                  JSONObject jsonResponse = new JSONObject(response);
                  boolean success = jsonResponse.getBoolean("success");
  
                  if (success) {
                      String username = jsonResponse.getString("username");
                      String email = jsonResponse.getString("email");
                      String gender = jsonResponse.getString("gender");
                      String country = jsonResponse.getString("country");
                      String verified = jsonResponse.getString("verified");
                      String avatar = jsonResponse.getString("avatar");
                      //String group = jsonResponse.getString("groups");
  
                      int numlength = 0;
                      int CI = 0;
  
                      JSONArray jsonArrayGR = jsonResponse.getJSONArray("groups");
                      //String array of 500 spots
                      String rows = "";
  
                      for (int i = 0; i &lt; jsonArrayGR.length(); i++){
                          CI++;
                          rows = jsonArrayGR.getString(i);
                      }
                      //int rows = jsonResponse.getInt("rows");
  
                      //Starting Activity
                      Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                      intent. addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  
                      intent.putExtra("username", username);
                      intent.putExtra("email", email);
                      intent.putExtra("gender", gender);
                      intent.putExtra("country", country);
                      intent.putExtra("verified", verified);
                      intent.putExtra("avatar", avatar);
                      intent.putExtra("groups", rows);
  
                      //intent.putExtra("rows", rows);
  
                      session.createUserSession(
                              username,
                              email,
                              gender,
                              country,
                              verified,
                              avatar,
                              rows
                      );
  
                      Toast.makeText(getBaseContext(), "Login succeed, welcome!" , Toast.LENGTH_LONG).show();
  
                      //Adding a new flag to start the activity
                      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                      LoginActivity.this.startActivity(intent);
                      _loginButton.setEnabled(true);
                      finish();
  
                  } else {
                      onLoginFailed();
                  }
              }
              catch (JSONException e){
                  e.printStackTrace();
              }
          }
  
      };
      LoginRequest loginRequest = new LoginRequest(e, password, responseListener);
      RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
      queue.add(loginRequest);
  </code></pre>
  
  <p>UserSession.java</p>
  
  <pre><code> public void createUserSession(
          String name,
          String email,
          String gender,
          String country,
          String verified,
          String avatar,
          String groups){
  
      //Storage_Start session
      editor.putBoolean(IS_USER_LOGIN, true);
  
      // Storing name in pref
      editor.putString(KEY_NAME, name);
  
      //Storage of the email in pref
      editor.putString(KEY_EMAIL, email);
  
      //Storing the gender
      editor.putString(KEY_GENDER, gender);
  
      //Storing the country
      editor.putString(KEY_COUNTRY, country);
  
      //Storing the verified
      editor.putString(KEY_VERIFIED, verified);
  
      //Storing avatar
      editor.putString(KEY_AVATAR, avatar);
  
      //Storing groups
      editor.putString(KEY_GROUPS, groups);
  
      //Storing the gRows
      //editor.putInt(INT_GROUPS, numlength);
  
      //commit changes
      editor.commit();
  }
  
  
  
  
  /**
   * Get stored session data
   * */
  public HashMap&lt;String, String&gt; getUserDetails(){
  
      //Use hashmap to store user credentials
      HashMap&lt;String, String&gt; user = new HashMap&lt;String, String&gt;();
  
      // user name
      user.put(KEY_NAME, pref.getString(KEY_NAME, null));
  
      // user email id
      user.put(KEY_EMAIL, pref.getString(KEY_EMAIL, null));
  
      //gender
      user.put(KEY_GENDER, pref.getString(KEY_GENDER, null));
  
      //country
      user.put(KEY_COUNTRY, pref.getString(KEY_COUNTRY, null));
  
      //verifed?
      user.put(KEY_VERIFIED, pref.getString(KEY_VERIFIED, null));
  
      //Avatar
      user.put(KEY_AVATAR, pref.getString(KEY_AVATAR, null));
  
      //Groups
      user.put(KEY_GROUPS, pref.getString(KEY_GROUPS, null));
  
      // return user
      return user;
  }
  </code></pre>
  
  <p><strong>UPDATE</strong>
  This is where I am storing the values into the recyclerview: </p>
  
  <pre><code>public class ChatsAdapter extends RecyclerView.Adapter&lt;ChatsAdapter.MyViewHolder&gt; {
  private List&lt;Groups&gt; chatlist;
  
  public class MyViewHolder extends RecyclerView.ViewHolder {
      public TextView group;
  
      public MyViewHolder(View view) {
          super(view);
          group = (TextView) view.findViewById(R.id.groups);
      }
  }
  
  public ChatsAdapter(List&lt;Groups&gt; chatlist){
      this.chatlist = chatlist;
  }
  @Override
  public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)    {
      View itemView = LayoutInflater.from(parent.getContext())
              .inflate(R.layout.chat_row_list, parent, false);
  
      return new MyViewHolder(itemView);
  }
  
  @Override
  public void onBindViewHolder(MyViewHolder holder, int position) {
      Groups groups = chatlist.get(position);
      holder.group.setText(groups.getTitle());
  }
  
  @Override
      public int getItemCount() {
          return chatlist.size();
     }
  }
  </code></pre>
  
  <p>And this is how I am defining the strings: </p>
  
  <pre><code> public class Groups {
      private String groups = "";
  
      public Groups() {
      }
  
       public Groups(String groups) {
           this.groups = groups;
       }
  
       public String getTitle() {
           return groups;
       }
     }
  </code></pre>
  
  <p>This is how I am displaying the values set by the adapter (above) in my main activity: </p>
  
  <pre><code>    //groups
      String g = user.get(UserSessionManager.KEY_GROUPS);
  
      String numrows = user.get(UserSessionManager.INT_GROUPS);
  
      //String grows = user.get(UserSessionManager.INT_GROUPS);
  
  
      int rows = Integer.parseInt(numrows);
  
      Groups groups = new Groups(g);
      groupslist.add(groups);
  
  
      chatsAdapter.notifyDataSetChanged();
  </code></pre>