title: How to store json object to shared preferences?
body: <p>I am creating an application,In my app I am getting response and displaing it in custom alert dialog, Till here it works fine, Now what I am trying to do is if user selects an item from alert dialog once, it should store it in preference so that no need of fetch the data every time.</p>
  
  <p>Following is my code snippet.</p>
  
  <p>The following response which i am getting is :</p>
  
  <pre><code>    {
      "states": [
          {
              "id": "1",
              "state_slug": "agra",
              "statename": "Agra"
          },
          {
              "id": "2",
              "state_slug": "andaman_and_nicobar_islands",
              "statename": "Andaman and Nicobar Islands"
          },
          {
              "id": "3",
              "state_slug": "andhra_pradesh",
              "statename": "Andhra Pradesh"
          },
          {
              "id": "4",
              "state_slug": "arunachal_pradesh",
              "statename": "Arunachal Pradesh"
          },
          {
              "id": "5",
              "state_slug": "assam",
              "statename": "Assam"
          },
          {
              "id": "6",
              "state_slug": "bihar",
              "statename": "Bihar"
          },
          {
              "id": "7",
              "state_slug": "bulandshahr",
              "statename": "Bulandshahr"
          },
          {
              "id": "8",
              "state_slug": "chhattisgarh",
              "statename": "Chhattisgarh"
          },
          {
              "id": "9",
              "state_slug": "dadra_and_nagar_haveli",
              "statename": "Dadra &amp; Nagar Haveli"
          },
          {
              "id": "10",
              "state_slug": "daman_and_diu",
              "statename": "Daman &amp; Diu"
          },
          {
              "id": "11",
              "state_slug": "delhi",
              "statename": "Delhi"
          },
          {
              "id": "12",
              "state_slug": "farrukhabad",
              "statename": "Farrukhabad"
          },
          {
              "id": "13",
              "state_slug": "goa",
              "statename": "Goa"
          },
          {
              "id": "14",
              "state_slug": "gujarat",
              "statename": "Gujarat"
          },
          {
              "id": "15",
              "state_slug": "haryana",
              "statename": "Haryana"
          },
          {
              "id": "16",
              "state_slug": "himachal_pradesh",
              "statename": "Himachal Pradesh"
          },
          {
              "id": "17",
              "state_slug": "jammu_and_kashmir",
              "statename": "Jammu &amp; Kashmir"
          },
          {
              "id": "18",
              "state_slug": "jharkhand",
              "statename": "Jharkhand"
          },
          {
              "id": "19",
              "state_slug": "karnataka",
              "statename": "Karnataka"
          },
          {
              "id": "20",
              "state_slug": "kerala",
              "statename": "Kerala"
          },
          {
              "id": "21",
              "state_slug": "lakshadweep",
              "statename": "Lakshadweep"
          },
          {
              "id": "22",
              "state_slug": "madhya_pradesh",
              "statename": "Madhya Pradesh"
          },
          {
              "id": "23",
              "state_slug": "maharashtra",
              "statename": "Maharashtra"
          },
          {
              "id": "24",
              "state_slug": "manipur",
              "statename": "Manipur"
          },
          {
              "id": "25",
              "state_slug": "meghalaya",
              "statename": "Meghalaya"
          },
          {
              "id": "26",
              "state_slug": "mizoram",
              "statename": "Mizoram"
          },
          {
              "id": "27",
              "state_slug": "nagaland",
              "statename": "Nagaland"
          },
          {
              "id": "28",
              "state_slug": "orissa",
              "statename": "Orissa"
          },
          {
              "id": "29",
              "state_slug": "pondicherry",
              "statename": "Pondicherry"
          },
          {
              "id": "30",
              "state_slug": "punjab",
              "statename": "Punjab"
          },
          {
              "id": "31",
              "state_slug": "purulia",
              "statename": "Purulia"
          },
          {
              "id": "32",
              "state_slug": "rajasthan",
              "statename": "Rajasthan"
          },
          {
              "id": "33",
              "state_slug": "sikkim",
              "statename": "Sikkim"
          },
          {
              "id": "34",
              "state_slug": "tamil_nadu",
              "statename": "Tamil Nadu"
          },
          {
              "id": "35",
              "state_slug": "tripura",
              "statename": "Tripura"
          },
          {
              "id": "36",
              "state_slug": "uttar_pradesh",
              "statename": "Uttar Pradesh"
          },
          {
              "id": "37",
              "state_slug": "uttarakhand",
              "statename": "Uttarakhand"
          },
          {
              "id": "38",
              "state_slug": "west_bengal",
              "statename": "West Bengal"
          }
      ]
  }
  </code></pre>
  
  <p>I am displaying statename in my alert dialog,</p>
  
  <pre><code>class LoadAllStates extends AsyncTask&lt;String, String, ArrayList&lt;HashMap&lt;String,String&gt;&gt;&gt; {
  
            ArrayAdapter&lt;String&gt; adapterallstates ;
          private ProgressDialog pDialog;
  
              @Override
              protected void onPreExecute() {
                  super.onPreExecute();
                  pDialog = new ProgressDialog(MainActivity.this);
                  pDialog.setMessage("Please wait..");
                  pDialog.setIndeterminate(true);
                 // pDialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.custom_progress));
                  pDialog.setCancelable(true);
                  pDialog.show();
  
              }
              protected ArrayList&lt;HashMap&lt;String,String&gt;&gt; doInBackground(String... args) {
                  ServiceHandler sh = new ServiceHandler();
  
                  // Making a request to url and getting response
                  statedata = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
                  String jsonStr = sh.makeServiceCall(STATE_URL, ServiceHandler.GET);
  
                  Log.d("Response: ", "&gt; " + jsonStr);
  
                  if (jsonStr != null) {
                      try {
                          JSONObject jsonObj = new JSONObject(jsonStr);
                          state_list = jsonObj.getJSONArray(COUNTRY_LIST);
  
                          // looping through All Contacts
                          for (int i = 0; i &lt; state_list.length(); i++) {
                              JSONObject c = state_list.getJSONObject(i);
  
                              // creating new HashMap
                              HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
  
                              // adding each child node to HashMap key =&gt; value
                                map.put(STATE_SLG, c.getString(STATE_SLG));
                            map.put(STATE_ID, c.getString(STATE_ID));
                              map.put(STATE_NAME,c.getString(STATE_NAME));
  
                             statedata.add(map);
  
                          }
                      } catch (JSONException e) {
                          e.printStackTrace();
                      }
                  } else {
                      Log.e("ServiceHandler", "Couldn't get any data from the url");
                  }
  
                  return statedata;
              }
  
              protected void onPostExecute(ArrayList&lt;HashMap&lt;String,String&gt;&gt; result) {
  
                  super.onPostExecute(result);
                 pDialog.dismiss();
  
                  String[] arrallstates=new String[statedata.size()];
                  for(int index=0;index&lt;statedata.size();index++){
                            HashMap&lt;String, String&gt; map=statedata.get(index);
                        arrallstates[index]=map.get(STATE_NAME);
                   }  
  
  
                   // pass arrConuntry array to ArrayAdapter&lt;String&gt; constroctor :
                  adapterallstates = new ArrayAdapter&lt;String&gt;(MainActivity.this,
                                      android.R.layout.simple_spinner_dropdown_item,
                                                                            arrallstates);
                  spstate.setOnClickListener(new OnClickListener() {
  
                      @Override
                      public void onClick(View w) {
                            new AlertDialog.Builder(MainActivity.this)
                            .setTitle("Select")
                            .setAdapter(adapterallstates, new DialogInterface.OnClickListener() {
  
                              @Override
                              public void onClick(DialogInterface dialog, int which) {
  
                                  spstate.setText(adapterallstates.getItem(which).toString());
  
                                   try {
  
                                       String n  = state_list.getJSONObject(which).getString("state_slug");
  
  
                                      statename=state_list.getJSONObject(which).getString("state_slug");
                                      stnm=state_list.getJSONObject(which).getString(STATE_NAME);
  
                                       Log.d("Response statenm: ", "&gt; " + statename);
  
                                       SharedPreferences.Editor editor = sharedpreferences.edit();
  
                                          editor.putString(States, n);
                                          editor.commit();
                                       new LoadAllStatesCity().execute();
  
                                      // Toast.makeText(getActivity(), statename, Toast.LENGTH_LONG).show();
                                  } catch (JSONException e) {
                                      // TODO Auto-generated catch block
                                      e.printStackTrace();
                                  }
  
                                dialog.dismiss();
                              }
                            }).create().show();
                          }
                  });
  
              }
       }
  </code></pre>